本文整理汇总了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);
}
}
示例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);
}
示例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);
}
示例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);
}
示例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));
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}