當前位置: 首頁>>代碼示例>>Java>>正文


Java WebAuthenticationDetailsSource類代碼示例

本文整理匯總了Java中org.springframework.security.web.authentication.WebAuthenticationDetailsSource的典型用法代碼示例。如果您正苦於以下問題:Java WebAuthenticationDetailsSource類的具體用法?Java WebAuthenticationDetailsSource怎麽用?Java WebAuthenticationDetailsSource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


WebAuthenticationDetailsSource類屬於org.springframework.security.web.authentication包,在下文中一共展示了WebAuthenticationDetailsSource類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: doFilter

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
		throws IOException, ServletException {
	HttpServletRequest httpServletRequest = (HttpServletRequest) request;

	String header_authorization = httpServletRequest.getHeader("Authorization");
	String token = (StringUtils.isBlank(header_authorization) ? null : header_authorization.split(" ")[1]);

	if (StringUtils.isBlank(header_authorization) && token == null) {
		logger.info("Token Not found in header.");
	} else {

		UserDetails principal = null;
		try {
			principal = authBuilder.getDefaultUserDetailsService().loadUserByUsername(token);
			UsernamePasswordAuthenticationToken userAuthenticationToken = new UsernamePasswordAuthenticationToken(
					principal, "", principal.getAuthorities());
			userAuthenticationToken
					.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
			SecurityContextHolder.getContext().setAuthentication(userAuthenticationToken);
		} catch (Exception e) {
			HttpServletResponse httpresposne = (HttpServletResponse) response;
			httpresposne.setContentType("application/json");
			httpresposne.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
			ObjectMapper jsonMapper = new ObjectMapper();
			PrintWriter out = httpresposne.getWriter();
			Map<String, String> jsonResponse = new HashMap<String, String>();
			jsonResponse.put("msg", "Invalid Token");
			out.write(jsonMapper.writeValueAsString(jsonResponse));
			out.flush();
			out.close();
			return;
		}
		chain.doFilter(request, response);
	}
}
 
開發者ID:PacktPublishing,項目名稱:Practical-Microservices,代碼行數:37,代碼來源:JwtAuthenticationTokenFilter.java

示例2: doFilterInternal

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    String token = request.getHeader(AUTH_HEADER);
    if (token != null && token.startsWith(BEARER_PREFIX)) {
    	token = token.substring(7);
    }
    String username = jwtTokenUtil.getUsernameFromToken(token);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {

        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
        
        if (jwtTokenUtil.tokenValido(token)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    chain.doFilter(request, response);
}
 
開發者ID:m4rciosouza,項目名稱:ponto-inteligente-api,代碼行數:22,代碼來源:JwtAuthenticationTokenFilter.java

示例3: doFilterInternal

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {

    String authToken = request.getHeader(this.tokenHeader);
    String username = jwtTokenUtil.getUsernameFromToken(authToken);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {

        // It is not compelling necessary to load the use details from the database. You could also store the information
        // in the token and read it from it. It's up to you ;)
        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);

        // For simple validation it is completely sufficient to just check the token integrity. You don't have to call
        // the database compellingly. Again it's up to you ;)
        if (jwtTokenUtil.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    filterChain.doFilter(request, response);

}
 
開發者ID:rodrigues882013,項目名稱:springboot-rest-api-skeleton,代碼行數:26,代碼來源:JwtAuthenticationTokenFilter.java

示例4: doFilter

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
		throws IOException, ServletException {
	HttpServletRequest httpRequest = (HttpServletRequest) request;
	String authToken = httpRequest.getHeader(this.tokenHeader);
	String username = this.tokenUtils.getUsernameFromToken(authToken);

	if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {

		UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
		if (this.tokenUtils.validateToken(authToken, userDetails)) {

			UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
					userDetails, null, userDetails.getAuthorities());
			authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));
			SecurityContextHolder.getContext().setAuthentication(authentication);
		}
	}

	chain.doFilter(request, response);
}
 
開發者ID:Mediv85,項目名稱:jwtExample,代碼行數:22,代碼來源:AuthenticationTokenFilter.java

示例5: doFilterInternal

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    String authToken = request.getHeader(this.tokenHeader);
    // authToken.startsWith("Bearer ")
    // String authToken = header.substring(7);
    String username = jwtTokenUtil.getUsernameFromToken(authToken);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
        logger.info("checking authentication for user " + username);
        // It is not compelling necessary to load the use details from the database. You could also store the information
        // in the token and read it from it. It's up to you ;)
        JwtUser userDetails = (JwtUser)this.userDetailsService.loadUserByUsername(username);

        // For simple validation it is completely sufficient to just check the token integrity. You don't have to call
        // the database compellingly. Again it's up to you ;)
        if (jwtTokenUtil.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            logger.info("authenticated user " + username + ", setting security context");
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    chain.doFilter(request, response);
}
 
開發者ID:zzqfsy,項目名稱:spring-jwt-starter,代碼行數:26,代碼來源:JwtAuthenticationTokenFilter.java

示例6: doFilterInternal

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    String authToken = request.getHeader(this.tokenHeader);
    // authToken.startsWith("Bearer ")
    // String authToken = header.substring(7);
    String username = jwtTokenUtil.getUsernameFromToken(authToken);

    logger.info("checking authentication user " + username);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {

        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);

        if (jwtTokenUtil.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            logger.info("authenticated user " + username + ", setting security context");
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    chain.doFilter(request, response);
}
 
開發者ID:quebic-source,項目名稱:microservices-sample-project,代碼行數:24,代碼來源:JwtAuthenticationTokenFilter.java

示例7: createAuthenticationToken

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@ApiOperation("get token")
@RequestMapping(value = "${jwt.route.authentication.path}", method = RequestMethod.POST)
public ResponseEntity<TokenRes> createAuthenticationToken(@Valid @ModelAttribute JwtAuthenticationReq authenticationRequest, HttpServletRequest httpServletRequest) throws AuthenticationException {

    // Perform the security
    String username = authenticationRequest.getUsername();
    UserDetails userDetails = userDetailsService.loadUserByUsername(username);

    if (!passwordEncoder.matches(authenticationRequest.getPassword(), userDetails.getPassword())) {
        throw new BadCredentialsException(username);
    }

    // For simple validation it is completely sufficient to just check the token integrity. You don't have to call
    // the database compellingly. Again it's up to you ;)
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
    authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
    LOGGER.info("authenticated user {}, setting security context", username);
    SecurityContextHolder.getContext().setAuthentication(authentication);

    // Reload password post-security so we can generate token
    String token = jwtTokenUtil.generateToken(username);
    LOGGER.info("username:{},token:{}", username, token);
    // Return the token
    return ResponseEntity.ok(new TokenRes(token));
}
 
開發者ID:helloworldtang,項目名稱:spring-boot-jwt-jpa,代碼行數:26,代碼來源:AuthenticationController.java

示例8: doFilter

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String authToken = httpRequest.getHeader(this.tokenHeader);
    String username = jwtTokenUtil.getUsernameFromToken(authToken);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
        if (jwtTokenUtil.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    chain.doFilter(request, response);
}
 
開發者ID:republique-et-canton-de-geneve,項目名稱:chvote-protocol-poc,代碼行數:19,代碼來源:JwtAuthenticationTokenFilter.java

示例9: doFilterInternal

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String authToken = httpRequest.getHeader(this.tokenHeader);
    // TODO: 10.09.16 have to determine how important this prefix is. maybe configurable?
    // authToken.startsWith("Bearer ")
    // String authToken = header.substring(7);
    String username = jwtTokenUtil.getUsernameFromToken(authToken);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
        if (jwtTokenUtil.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    chain.doFilter(request, response);
}
 
開發者ID:ButterSpring,項目名稱:jwt-spring-boot-security,代碼行數:22,代碼來源:JwtAuthenticationTokenFilter.java

示例10: doFilterInternal

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    String authToken = request.getHeader(AUTH_HEADER_NAME);
    String username = jwtTokenUtil.getUsernameFromToken(authToken);

    logger1.info("checking authentication find user " + username);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
        if (jwtTokenUtil.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            logger1.info("authenticated user " + username + ", setting security context");
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    chain.doFilter(request, response);
}
 
開發者ID:nikhilmetrani,項目名稱:alpha-umi,代碼行數:20,代碼來源:JwtAuthenticationTokenFilter.java

示例11: doFilterInternal

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
    FilterChain chain) throws ServletException, IOException {
  String authToken = request.getHeader(this.tokenHeader);
  if (authToken != null && authToken.startsWith(TOKEN_PREFIX)) {
    authToken = authToken.substring(TOKEN_PREFIX.length());
    String username = jwtService.getUsernameFromToken(authToken);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
      UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);

      if (jwtService.validateToken(authToken, userDetails)) {
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
            userDetails, null, userDetails.getAuthorities());
        authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
        logger.info("authenticated user " + username + ", setting security context");
        SecurityContextHolder.getContext().setAuthentication(authentication);
      }
    }
  }

  chain.doFilter(request, response);
}
 
開發者ID:empt-ak,項目名稱:meditor,代碼行數:24,代碼來源:JwtAuthTokenFilter.java

示例12: doFilterInternal

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
/**
 * This method will be be invoked once per request within a single request thread.
 * Base method which is used to check user authorities using tokens during any request.
 * <p>
 * Implementation of basic {@link org.springframework.web.filter.OncePerRequestFilter
 * #doFilterInternal(HttpServletRequest, HttpServletResponse, FilterChain)}  method.
 *
 * @param request  the request, in which method will be executed
 * @param response the response
 * @param chain    an object provided by the servlet container to the developer
 *                 giving a view into the invocation chain of a filtered request
 *                 for a resource
 * @throws ServletException if {@code request} or {@code response} are not {@link HttpServletRequest}
 *                          or {@link HttpServletResponse} type accordingly
 * @throws IOException      on input error
 */
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    String authToken = request.getHeader(this.tokenHeader);
    String username = jwtTokenGenerationService.getUsernameFromToken(authToken);

    if (username != null) {
        LOG.info(String.format("Checking authentication for user %s ", username));
        try {
            JWTUser jwtUser = this.userDetailsService.loadUserByUsername(username);
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            LOG.info(String.format("Authenticated user %s, setting security context", username));
            LOG.info(String.format("%s has authorities: %s", username, jwtUser.getAuthorities()));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        } catch (UsernameNotFoundException e) {
            LOG.info(String.format("User %s not found.", username));
        }
    }

    chain.doFilter(request, response);
}
 
開發者ID:bulktrade,項目名稱:SMSC,代碼行數:38,代碼來源:JWTAuthenticationTokenFilter.java

示例13: doFilterInternal

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    String token = request.getHeader(tokenHeader);
    if (token != null ) {
        String username = jwtTokenUtil.getUsernameFromToken(token);
        logger.info("checking authentication " + username);

        if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {

            // 如果我們足夠相信token中的數據,也就是我們足夠相信簽名token的secret的機製足夠好
            // 這種情況下,我們可以不用再查詢數據庫,而直接采用token中的數據
            // 本例中,我們還是通過Spring Security的 @UserDetailsService 進行了數據查詢
            // 但簡單驗證的話,你可以采用直接驗證token是否合法來避免昂貴的數據查詢
            UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);

            if (jwtTokenUtil.validateToken(token, userDetails)) {
                UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
                authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                logger.info("authenticated user " + username + ", setting security context");
                SecurityContextHolder.getContext().setAuthentication(authentication);
            }
        }
    }

    chain.doFilter(request, response);
}
 
開發者ID:ToQuery,項目名稱:CleverWeb,代碼行數:27,代碼來源:JwtAuthenticationTokenFilter.java

示例14: doFilterInternal

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    String authToken = request.getHeader(this.tokenHeader);
    String username = jwtTokenUtil.getUsernameFromToken(authToken);

    logger.info("checking authentication für user " + username);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {

        // It is not compelling necessary to load the use details from the database. You could also store the information
        // in the token and read it from it. It's up to you ;)
        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);

        // For simple validation it is completely sufficient to just check the token integrity. You don't have to call
        // the database compellingly. Again it's up to you ;)
        if (jwtTokenUtil.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            logger.info("authenticated user " + username + ", setting security context");
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    chain.doFilter(request, response);
}
 
開發者ID:HiOA-ABI,項目名稱:nikita-noark5-core,代碼行數:26,代碼來源:JwtAuthenticationTokenFilter.java

示例15: doFilter

import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; //導入依賴的package包/類
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    final HttpServletRequest httpRequest = (HttpServletRequest) request;
    final String header = httpRequest.getHeader("Authorization");
    final SecurityContext context = SecurityContextHolder.getContext();
    if (header != null && context.getAuthentication() == null) {
        final String tokenStr = header.substring("Bearer ".length());
        final JwtToken token = jwtTokenCodec.decodeToken(tokenStr);
        if (!token.isExpired()) {
            final PreAuthenticatedAuthenticationToken authentication = new PreAuthenticatedAuthenticationToken(token, "n/a", token.getRoles().stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));
            context.setAuthentication(authentication);
        }
    }
    chain.doFilter(request, response);
}
 
開發者ID:nidi3,項目名稱:jwt-with-spring,代碼行數:17,代碼來源:JwtAuthenticationTokenFilter.java


注:本文中的org.springframework.security.web.authentication.WebAuthenticationDetailsSource類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。