过滤网址模式而不进行身份验证?

时间:2018-05-18 09:49:24

标签: spring spring-mvc spring-boot spring-security

请设置Spring Security,请帮助我。 我找到了类似的东西,但它对我来说不太适合.. https://stackoverflow.com/a/36875726/1590594

配置指定必须对每个请求进行身份验证。 有必要执行以下操作,即在指定的URL(" / push")上只使用一个过滤器。过滤器进行适当的检查并进一步跳过请求或拒绝。没有身份验证。

    @Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().
            sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and().
            authorizeRequests()
            .anyRequest().authenticated().
            and().
            anonymous().disable().
            exceptionHandling().authenticationEntryPoint(unauthorizedEntryPoint());
    http.addFilterBefore(new UserAuthenticationFilter(authenticationManager()), BasicAuthenticationFilter.class);
    http.authorizeRequests().antMatchers(HttpMethod.POST, "/push").authenticated().and().addFilterBefore(new RPushFilter(),BasicAuthenticationFilter.class);
}

并过滤

    public class RPushFilter extends GenericFilterBean {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        //IF NOT httpResponse.sendError(HttpStatus.BAD_REQUEST.value(), "Access denied");

        chain.doFilter(request, response);
    }
}

0 个答案:

没有答案