本文整理汇总了Java中org.springframework.security.core.Authentication.isAuthenticated方法的典型用法代码示例。如果您正苦于以下问题:Java Authentication.isAuthenticated方法的具体用法?Java Authentication.isAuthenticated怎么用?Java Authentication.isAuthenticated使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.security.core.Authentication
的用法示例。
在下文中一共展示了Authentication.isAuthenticated方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: authenticationIsRequired
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
private boolean authenticationIsRequired(String username) {
Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication();
if (Objects.isNull(existingAuth) || !existingAuth.isAuthenticated()) {
return true;
}
if (existingAuth instanceof UsernamePasswordAuthenticationToken
&& !existingAuth.getName().equals(username)) {
return true;
}
if (existingAuth instanceof AnonymousAuthenticationToken) {
return true;
}
return false;
}
示例2: globalAttributes
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@ModelAttribute
public void globalAttributes(Model model) {
Boolean isAllowedToUpdateMovieDB = true;
String displayName = "Anonymous";
if (facebookAppId != null && !facebookAppId.isEmpty()) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth instanceof OAuth2Authentication) {
displayName = (String) ((LinkedHashMap) ((OAuth2Authentication) auth).getUserAuthentication().getDetails()).get("name");
isAllowedToUpdateMovieDB = auth.isAuthenticated();
} else {
isAllowedToUpdateMovieDB = false;
}
}
UserInfo userInfo = new UserInfo(displayName, isAllowedToUpdateMovieDB);
model.addAttribute("userInfo", userInfo);
}
示例3: preHandle
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
boolean isAuthenticated;
if (authentication != null) {
isAuthenticated = authentication instanceof AnonymousAuthenticationToken ? false
: authentication.isAuthenticated();
if (isAuthenticated) {
response.setContentType("text/plain");
sendRedirect(request, response);
return false; // no need to proceed with the chain as we already dealt with the response
}
}
return true;
}
示例4: auditorAware
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Bean
public AuditorAware<String> auditorAware() {
return new AuditorAware<String>() {
@Override
public String getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
return null;
}
return authentication.getName();
}
};
}
示例5: doFilter
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
SecurityContext context = (SecurityContext) req.getSession().getAttribute(SPRING_SECURITY_CONTEXT_KEY);
if (context != null) {
Authentication authentication = context.getAuthentication();
if (authentication instanceof OAuth2Authentication && authentication.isAuthenticated()) {
authentication = ((OAuth2Authentication) authentication).getUserAuthentication();
if (authentication != null) {
DiscordUserDetails details = SecurityUtils.getDetails(authentication);
if (details != null) {
MDC.put("userId", details.getId());
}
}
}
}
chain.doFilter(request, response);
}
示例6: logout
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@DeleteMapping(value = "/api/session", produces = CONTENT_TYPE)
public String logout() {
logger.info("用户登出: 正在检测登入状态");
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
if (authentication != null && authentication.isAuthenticated()) {
String username = authentication.getName();
context.setAuthentication(null);
if (!"anonymousUser".equals(username)) {
logger.info("用户登出: 用户已成功登出, username={}", username);
return booleanResult(true);
} else {
logger.info("用户登出: 检测到匿名用户");
return booleanResult(true);
}
} else {
logger.info("用户登出: 未检测到已登入状态");
return booleanResult(false);
}
}
示例7: login
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Override
public boolean login(String email, String password) {
try {
Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(email, password));
if (authenticate.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(authenticate);
return true;
}
} catch (AuthenticationException e) {
}
return false;
}
示例8: isUserLoggedIn
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
/**
* Checks if is user logged in.
*
* @return true, if is user logged in
*/
protected static boolean isUserLoggedIn() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (null != authentication) {
return (authentication.isAuthenticated() && !(authentication instanceof AnonymousAuthenticationToken));
} else {
return false;
}
}
示例9: getUser
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
private UserContext getUser(SecurityContext context) {
if (context == null) return null;
Authentication authentication = context.getAuthentication();
if (authentication != null && authentication.isAuthenticated() && authentication.getPrincipal() instanceof UserContext)
return (UserContext)authentication.getPrincipal();
return null;
}
示例10: authenticate
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
Authentication authenticationResult = authenticationManager
.authenticate(authentication);
if (authenticationResult.isAuthenticated()) {
// validates nonce because JWT is already valid
if (authentication instanceof PoPAuthenticationToken) {
PoPAuthenticationToken popAuthentication = (PoPAuthenticationToken) authentication;
// starts validating nonce here
String nonce = popAuthentication.getNonce();
if (nonce == null) {
throw new UnapprovedClientAuthenticationException(
"This request does not have a valid signed nonce");
}
String token = (String) popAuthentication.getPrincipal();
System.out.println("access token:" + token);
try {
JWT jwt = JWTParser.parse(token);
String publicKey = jwt.getJWTClaimsSet().getClaim("public_key").toString();
JWK jwk = JWK.parse(publicKey);
JWSObject jwsNonce = JWSObject.parse(nonce);
JWSVerifier verifier = new RSASSAVerifier((RSAKey) jwk);
if (!jwsNonce.verify(verifier)) {
throw new InvalidTokenException("Client hasn't possession of given token");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return authenticationResult;
}
示例11: isLogin
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
/**
* 是否登录
* <p>
* {@link Authentication#isAuthenticated()}
*
* @return <code>true</code> , 匿名用户(未登录)返回 <code>false</code>
*/
public static boolean isLogin () {
Authentication authentication = getAuthentication();
if ( authentication.getAuthorities().contains( ROLE_ANONYMOUS )
|| ANONYMOUS_USER_PRINCIPAL.equals( authentication.getPrincipal() ) ) {
return false;
}
return authentication.isAuthenticated();
}
示例12: isLoggedIn
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
/**
* Check if the current user is authenticated (logged in) not anonymously.
* Use in thymeleaf with th:if="${@yadaWebUtil.loggedIn()}"
* @return
*/
@Deprecated // use isLoggedIn() instead
public boolean loggedIn() {
try {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth!=null && auth.isAuthenticated()) {
Object principal = auth.getPrincipal();
return (principal instanceof UserDetails);
}
} catch (Exception e) {
log.error("Can't get Authentication object", e);
}
return false;
}
示例13: getValidAuthentication
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
protected Authentication getValidAuthentication() {
SecurityContext ctx = getSecurityContext();
if (ctx != null) {
Authentication authc = ctx.getAuthentication();
if (authc != null && !(authc instanceof AnonymousAuthenticationToken) && authc.isAuthenticated()) {
return authc;
}
}
return null;
}
示例14: getTokenAuthentication
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
public static OAuth2Authentication getTokenAuthentication() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.isAuthenticated() && auth instanceof OAuth2Authentication) {
return (OAuth2Authentication) auth;
}
return null;
}
示例15: login
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(HttpServletRequest request, HttpServletResponse response, Model model) {
HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response);
httpSessionSecurityContextRepository.loadContext(holder);
try {
// 使用提供的证书认证用户
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER", "ROLE_ADMIN");
Authentication auth = new UsernamePasswordAuthenticationToken(request.getParameter("username"), request.getParameter("password"), authorities);
SecurityContextHolder.getContext().setAuthentication(authenticationManager.authenticate(auth));
// 认证用户
if(!auth.isAuthenticated())
throw new CredentialException("用户不能够被认证");
} catch (Exception ex) {
// 用户不能够被认证,重定向回登录页
logger.info(ex);
return "login";
}
// 从会话得到默认保存的请求
DefaultSavedRequest defaultSavedRequest = (DefaultSavedRequest) request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST");
// 为令牌请求生成认证参数Map
Map<String, String> authParams = getAuthParameters(defaultSavedRequest);
AuthorizationRequest authRequest = new DefaultOAuth2RequestFactory(clientDetailsService).createAuthorizationRequest(authParams);
authRequest.setAuthorities(AuthorityUtils.createAuthorityList("ROLE_USER", "ROLE_ADMIN"));
model.addAttribute("authorizationRequest", authRequest);
httpSessionSecurityContextRepository.saveContext(SecurityContextHolder.getContext(), holder.getRequest(), holder.getResponse());
return "authorize";
}