Spring boot 弹簧安全过滤器未启动

Spring boot 弹簧安全过滤器未启动,spring-boot,spring-security,Spring Boot,Spring Security,我的身份验证筛选器未根据请求启动 我有两个安全配置,一个仅用于登录端点,使用AuthenticationFromCredentialsFilter过滤器进行身份验证,另一个用于其他端点,使用AuthenticationFromTokenFilter过滤器进行身份验证 我希望调用过滤器的attemptAuthentication方法,但它们不是 在筛选器中而不是在登录控制器中对凭据进行身份验证和创建令牌有何意义 登录控制器暂时存在,但它不应该存在,因为它的工作应该由过滤器完成 我在安全配置中分别设

我的身份验证筛选器未根据请求启动

我有两个安全配置,一个仅用于登录端点,使用
AuthenticationFromCredentialsFilter
过滤器进行身份验证,另一个用于其他端点,使用
AuthenticationFromTokenFilter
过滤器进行身份验证

我希望调用过滤器的
attemptAuthentication
方法,但它们不是

在筛选器中而不是在登录控制器中对凭据进行身份验证和创建令牌有何意义

登录控制器暂时存在,但它不应该存在,因为它的工作应该由过滤器完成

我在安全配置中分别设置了它们:

@EnvProd
@启用Web安全性
@ComponentScan(nameGenerator=PackageBeanNameGenerator.class,basePackages={“com.thalasoft.user.rest.security”,“com.thalasoft.user.rest.filter”})
公共类Web安全配置{
@订单(1)
@配置
公共类凭据配置扩展了WebSecurity配置适配器{
公共AuthenticationManager authenticationManagerBean()引发异常{
返回super.authenticationManagerBean();
}
公共身份验证FromCredentialsFilter身份验证FromCredentialsFilter()引发异常{
AuthenticationFromCredentialsFilter AuthenticationFromCredentialsFilter=新建AuthenticationFromCredentialsFilter();
authenticationFromCredentialsFilter.setAuthenticationManager(authenticationManagerBean());
从CredentialsFilter返回authenticationFromCredentialsFilter;
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http.antMatcher(“/api/users/login”)
.addFilterBefore(authenticationFromCredentialsFilter(),UsernamePasswordAuthenticationFilter.class)
.授权请求()
.antMatchers(“/api/users/login”).permitAll()
.anyRequest().authenticated();
}
}
@订单(2)
@配置
公共类令牌配置扩展了WebSecurity配置适配器{
@自动连线
私有身份验证FromTokenFilter身份验证FromTokenFilter;
@自动连线
私有重新验证入口点重新验证入口点;
@自动连线
专用SimpleCORSFilter SimpleCORSFilter;
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http
.csrf().disable();
http
.sessionManagement().sessionCreationPolicy(sessionCreationPolicy.STATELESS);
http
.headers()
.cacheControl().disable()
.frameOptions().disable();
http
.httpBasic()
.authenticationEntryPoint(重新验证EntryPoint);
http
.addFilterBefore(simpleCORSFilter,UsernamePasswordAuthenticationFilter.class);
http
.addFilterBefore(authenticationFromTokenFilter,UsernamePasswordAuthenticationFilter.class);
http.antMatcher(“/api/**”)
.addFilterBefore(authenticationFromTokenFilter(),UsernamePasswordAuthenticationFilter.class)
.授权请求()
.antMatchers(“/”).permitAll()
.antMatchers(“/error”).permitAll()
.antMatchers(“/admin/**”).hasRole(UserDomainConstants.ROLE\u admin)
.anyRequest().authenticated();
}
}
}
以下是两个过滤器:

公共类AuthenticationFromCredentialsFilter扩展了UsernamePasswordAuthenticationFilter{
@自动连线
私人AuthenticationManager AuthenticationManager;
@自动连线
专用TokenAuthenticationService TokenAuthenticationService;
@自动连线
证书服务证书服务;
@凌驾
公共身份验证尝试身份验证(HttpServletRequest-req、HttpServletResponse-res)
抛出AuthenticationException{
试一试{
CredentialsResource CredentialsResource=new ObjectMapper().readValue(req.getInputStream(),
凭证资源(类别);
返回authenticationManager.authenticate(credentialsService.authenticate(credentialsResource));
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
@凌驾
受保护的无效成功身份验证(HttpServletRequest请求、HttpServletResponse响应、FilterChain链、,
身份验证)引发IOException、ServletException{
addTokenToResponseHeader(响应、身份验证);
}
}
公共类AuthenticationFromTokenFilter扩展UsernamePasswordAuthenticationFilter{
@自动连线
私人AuthenticationManager AuthenticationManager;
@自动连线
专用TokenAuthenticationService TokenAuthenticationService;
@凌驾
公共身份验证尝试身份验证(HttpServletRequest请求、HttpServletResponse响应)
抛出AuthenticationException{
tokenAuthenticationService.Authentication(请求);
返回authenticationManager.authenticate(tokenAuthenticationService.authenticate(请求));
}
@凌驾
受保护的无效成功身份验证(HttpServletRequest请求、HttpServletResponse响应、FilterChain链、,
身份验证)引发IOException、ServletException{
}
}
下面是一个登录请求的示例,该登录请求应该由安全配置中的
AuthenticationFromCredentialsFilter
过滤器捕获,但不被捕获,因此允许进入控制器并给出状态为
201
的响应:

$ curl -i -H "Accept:application/json" -H "Content-Type: application/json" "http://localhost:8080/api/users/login" -X POST -d "{ \"email\" : \"xxxxxx@yahoo.se\", \"password\" : \"xxxxx\" }"
HTTP/1.1 201 
Cache-Control: no-store
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MzQwNTE5MjYsInN1YiI6Im1pdHRpcHJvdmVuY2VAeWFob28uc2UifQ.LOJvr5jWouWsLN_Pinlr_F5dntON45hwpUFVmXD2Xqo
Location: http://localhost:8080/api/users/1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 05 Aug 2018 05:32:07 GMT


{"firstname":"Stephane","lastname":"Eybert","email":"xxxxx@yahoo.se","confirmedEmail":false,"password":"bWl0dGlwcm92ZW5jZUB5YWhvby5zZTptaWduZXQxYjE4ZDQ5MS00ZGRhLTQxZWYtYWM5ZS04N2Y5ODk=","workPhone":null,"userRoles":[{"role":"ROLE_ADMIN","id":1}],"_links":{"self":{"href":"http://localhost:8080/api/users/1"},"roles":{"href":"http://localhost:8080/api/users/1/roles"}},"id":1}[stephane@stephane-ThinkPad-X201 user-rest (master)]
$ curl -i -H "Accept:application/json" -H "Content-Type: application/json" "http://localhost:8080/api/users/1/password" -X PUT -d "\"xxxxx\""
HTTP/1.1 200 
Cache-Control: no-store
Location: http://localhost:8080/api/users/1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 04 Aug 2018 20:23:17 GMT

{"firstname":"Stephane","lastname":"Eybert","email":"xxxx@yahoo.se","confirmedEmail":false,"password":"bWl0dGlwcm92ZW5jZUB5YWhvby5zZTptaWduZXRhYTA4OTNiZS0yMzZlLTQ3ZjktOTE2Ny0zOTU0NTY=","workPhone":null,"userRoles":[{"role":"ROLE_ADMIN","id":1}],"_links":{"self":{"href":"http://localhost:8080/api/users/1"},"roles":{"href":"http://localhost:8080/api/users/1/roles"}},"id":1}
我期望登录请求触发
AuthenticationFromCredentialsFi,对吗
@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(customAuthenticationProvider);
}
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    CredentialsService credentialsService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        return credentialsService.authenticate(authentication);
    }

    @Override
    public boolean supports(Class<?> authentication) {
        boolean value = (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
        return value;
    }

}
public AuthenticationFromCredentialsFilter authenticationFromCredentialsFilter() throws Exception {
    AuthenticationFromCredentialsFilter authenticationFromCredentialsFilter = new AuthenticationFromCredentialsFilter();
    authenticationFromCredentialsFilter.setAuthenticationManager(authenticationManagerBean());
    authenticationFromCredentialsFilter.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/api/users/login"));
    return authenticationFromCredentialsFilter;
}

public AuthenticationFromTokenFilter authenticationFromTokenFilter() throws Exception {
    AuthenticationFromTokenFilter authenticationFromTokenFilter = new AuthenticationFromTokenFilter();
    authenticationFromTokenFilter.setAuthenticationManager(authenticationManagerBean());
    authenticationFromTokenFilter.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/**"));
    return authenticationFromTokenFilter;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();

    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    http.headers().cacheControl().disable().frameOptions().disable();

    http.httpBasic().authenticationEntryPoint(restAuthenticationEntryPoint);

    http.addFilterBefore(simpleCORSFilter, UsernamePasswordAuthenticationFilter.class);

    http.antMatcher("/api/**")
    .addFilterBefore(authenticationFromCredentialsFilter(), UsernamePasswordAuthenticationFilter.class)
    .addFilterBefore(authenticationFromTokenFilter(), UsernamePasswordAuthenticationFilter.class)
    .authorizeRequests()
    .antMatchers("/").permitAll()
    .antMatchers("/error").permitAll()
    .antMatchers("/api/users/login").permitAll()
    .antMatchers("/admin/**").hasRole(UserDomainConstants.ROLE_ADMIN)
    .anyRequest().authenticated();
}