【发布时间】:2016-02-23 20:08:03
【问题描述】:
我正在尝试使用我自己实现的JWT 来保护我的 REST 服务器(这意味着 JWT 中没有弹簧东西可以自行处理,其他一切当然是Spring)。
我有这门课:JWTToken implements Authentication。
我有一个过滤器负责在SecurityContextHolder 设置JWTToken 实例:
public class JwtFilter extends GenericFilterBean {
public void doFilter(...) {
....
JWTToken token = new JWTToken(jwt); // this init the Authentication object with all the jwt claims
SecurityContextHolder.getContext().setAuthentication(token);
....
}
我也有一个调试资源:
@RequestMapping(
value = "/protected_resource",
method = RequestMethod.POST
)
@RolesAllowed("admin")
public RESTResponse<String> debugJwt() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); // here I can see that the context is the right one
return new RESTResponse<>("This was successful", "feedback message", true);
}
我错过了一个我在任何在线资源中都找不到的难题,这是如何实现WebSecurityConfigurerAdapter,特别是configure(HttpSecurity http)方法。
当我尝试这样做时,例如:
http.authorizeRequests().anyRequest().authenticated()
请求没有通过这里,资源没有被调用。
我在这里错过了什么?
【问题讨论】: