【发布时间】:2018-10-30 08:30:22
【问题描述】:
我目前正在处理 Angular 4 FrontEnd App 和 Spring Boot Backend App 之间的 JWT 身份验证,服务器端的一切工作正常,我在身份验证级别遇到的问题如下。
这发生在我获得登录请求已成功处理的 200 OK 状态后,如下图所示。
在我用于安全应用程序和登录处理的配置下方。
public class JWTAuthorizationFiler extends OncePerRequestFilter {
@SuppressWarnings("unchecked")
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, "
+ "Access-Control-Request-Method, Access-Control-Request-Headers, authorization");
response.addHeader("Access-Control-Expose-Headers",
"Access-Control-Allow-Origin, Access-Control-Allow-Credentials, authorization");
if (request.getMethod().equals("OPTIONS")) {
response.setStatus(HttpServletResponse.SC_OK);
}
String jwt = request.getHeader(SecurityConstants.HEADER_STRING);
if (jwt == null || !jwt.startsWith(SecurityConstants.TOKEN_PREFIX)) {
filterChain.doFilter(request, response);
return;
}
Claims claims = Jwts.parser().setSigningKey(SecurityConstants.SECRET)
.parseClaimsJws(jwt.replace(SecurityConstants.TOKEN_PREFIX, "")).getBody();
String username = claims.getSubject();
ArrayList<Map<String, String>> roles = (ArrayList<Map<String, String>>) claims.get("roles");
Collection<GrantedAuthority> authorities = new ArrayList<>();
roles.forEach(r -> {
authorities.add(new SimpleGrantedAuthority(r.get("authority")));
});
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username,
null, authorities);
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
filterChain.doFilter(request, response);
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private BCryptPasswordEncoder bCyptPasswordEncoder;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCyptPasswordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// http.formLogin();
http.authorizeRequests().antMatchers("/login/**", "/register/**", "/paramsApi/**").permitAll();
http.authorizeRequests().antMatchers(HttpMethod.POST, "/studentResource/**").hasAuthority("ADMIN");
http.authorizeRequests().antMatchers(HttpMethod.GET, "/studentResource/**").hasAuthority("ADMIN");
http.authorizeRequests().anyRequest().authenticated();
http.addFilter(new JWTAuthenticationFilter(authenticationManager()));
http.addFilterBefore(new JWTAuthorizationFiler(), UsernamePasswordAuthenticationFilter.class);
}
}
我认为 JWTAuthorizationFilter.java 是这个问题的主要原因,非常感谢任何解决这个问题的方法。
【问题讨论】:
标签: angular spring-boot spring-security cors jwt