当前位置: 首页>>代码示例>>Java>>正文


Java Authentication.isAuthenticated方法代码示例

本文整理汇总了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;
}
 
开发者ID:revinate,项目名称:grpc-spring-security-demo,代码行数:18,代码来源:BasicAuthenticationInterceptor.java

示例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);
}
 
开发者ID:Microsoft,项目名称:movie-db-java-on-azure,代码行数:19,代码来源:GlobalControllerAdvice.java

示例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;
}
 
开发者ID:Azanx,项目名称:Smart-Shopping,代码行数:18,代码来源:RedirectWhenAuthenticatedInterceptor.java

示例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();
        }
    };
}
 
开发者ID:weechang,项目名称:Taroco,代码行数:16,代码来源:ApiGateWayApplication.java

示例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);
}
 
开发者ID:GoldRenard,项目名称:JuniperBotJ,代码行数:19,代码来源:InfoMdcFilter.java

示例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);
    }
}
 
开发者ID:mingzuozhibi,项目名称:mzzb-server,代码行数:22,代码来源:SessionController.java

示例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;
}
 
开发者ID:zralock,项目名称:CTUConference,代码行数:13,代码来源:AuthenticationServiceImpl.java

示例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;
    }

}
 
开发者ID:telstra,项目名称:open-kilda,代码行数:15,代码来源:BaseController.java

示例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;
}
 
开发者ID:Jenner4S,项目名称:unitimes,代码行数:8,代码来源:SessionsTag.java

示例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;
}
 
开发者ID:PacktPublishing,项目名称:OAuth-2.0-Cookbook,代码行数:42,代码来源:PoPAuthenticationManager.java

示例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();
}
 
开发者ID:yujunhao8831,项目名称:spring-boot-start-current,代码行数:16,代码来源:ContextUtils.java

示例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;
}
 
开发者ID:xtianus,项目名称:yadaframework,代码行数:19,代码来源:YadaSecurityUtil.java

示例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;
}
 
开发者ID:juiser,项目名称:juiser,代码行数:11,代码来源:SecurityContextUser.java

示例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;
}
 
开发者ID:GoldRenard,项目名称:JuniperBotJ,代码行数:8,代码来源:SecurityUtils.java

示例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";
}
 
开发者ID:chaokunyang,项目名称:microservices-event-sourcing,代码行数:32,代码来源:LoginController.java


注:本文中的org.springframework.security.core.Authentication.isAuthenticated方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。