如何禁用Spring Security对特定URL的保护

117

我正在使用无状态的Spring Security,但在注册时我想禁用Spring Security。我已经通过以下方式禁用了它:

antMatchers("/api/v1/signup").permitAll().

但它没有起作用,我得到以下错误:

 message=An Authentication object was not found in the SecurityContext, type=org.springframework.security.authentication.AuthenticationCredentialsNotFoundException

我认为这意味着Spring安全过滤器正在工作。

我的URL顺序将始终为“/api/v1”。

我的Spring配置是:

@Override
    protected void configure(HttpSecurity http) throws Exception {

         http.
         csrf().disable().
         sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).
         and().
         authorizeRequests().
         antMatchers("/api/v1/signup").permitAll().
         anyRequest().authenticated().
         and().
         anonymous().disable();
        http.addFilterBefore(new AuthenticationFilter(authenticationManager()), BasicAuthenticationFilter.class);
    }

我的认证过滤器是

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

        String username = httpRequest.getHeader("X-Auth-Username");
        String password = httpRequest.getHeader("X-Auth-Password");
        String token = httpRequest.getHeader("X-Auth-Token");

        String resourcePath = new UrlPathHelper().getPathWithinApplication(httpRequest);

        try {

            if (postToAuthenticate(httpRequest, resourcePath)) {            
                processUsernamePasswordAuthentication(httpResponse, username, password);
                return;
            }

            if(token != null){
                processTokenAuthentication(token);
            }
            chain.doFilter(request, response);
        } catch (InternalAuthenticationServiceException internalAuthenticationServiceException) {
            SecurityContextHolder.clearContext();
            logger.error("Internal authentication service exception", internalAuthenticationServiceException);
            httpResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } catch (AuthenticationException authenticationException) {
            SecurityContextHolder.clearContext();
            httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, authenticationException.getMessage());
        } finally {
        }
    }

     private HttpServletRequest asHttp(ServletRequest request) {
            return (HttpServletRequest) request;
        }

        private HttpServletResponse asHttp(ServletResponse response) {
            return (HttpServletResponse) response;
        }

        private boolean postToAuthenticate(HttpServletRequest httpRequest, String resourcePath) {
            return Constant.AUTHENTICATE_URL.equalsIgnoreCase(resourcePath) && httpRequest.getMethod().equals("POST");
        }

        private void processUsernamePasswordAuthentication(HttpServletResponse httpResponse,String username, String password) throws IOException {
            Authentication resultOfAuthentication = tryToAuthenticateWithUsernameAndPassword(username, password);
            SecurityContextHolder.getContext().setAuthentication(resultOfAuthentication);
            httpResponse.setStatus(HttpServletResponse.SC_OK);
            httpResponse.addHeader("Content-Type", "application/json");
            httpResponse.addHeader("X-Auth-Token", resultOfAuthentication.getDetails().toString());
        }

        private Authentication tryToAuthenticateWithUsernameAndPassword(String username,String password) {
            UsernamePasswordAuthenticationToken requestAuthentication = new UsernamePasswordAuthenticationToken(username, password);
            return tryToAuthenticate(requestAuthentication);
        }

        private void processTokenAuthentication(String token) {
            Authentication resultOfAuthentication = tryToAuthenticateWithToken(token);
            SecurityContextHolder.getContext().setAuthentication(resultOfAuthentication);
        }

        private Authentication tryToAuthenticateWithToken(String token) {
            PreAuthenticatedAuthenticationToken requestAuthentication = new PreAuthenticatedAuthenticationToken(token, null);
            return tryToAuthenticate(requestAuthentication);
        }

        private Authentication tryToAuthenticate(Authentication requestAuthentication) {
            Authentication responseAuthentication = authenticationManager.authenticate(requestAuthentication);
            if (responseAuthentication == null || !responseAuthentication.isAuthenticated()) {
                throw new InternalAuthenticationServiceException("Unable to authenticate Domain User for provided credentials");
            }
            logger.debug("User successfully authenticated");
            return responseAuthentication;
        }

我的控制器是

@RestController
public class UserController {

    @Autowired
    UserService userService;

    /**
     * to pass user info to service
     */
    @RequestMapping(value = "api/v1/signup",method = RequestMethod.POST)
    public String saveUser(@RequestBody User user) {
        userService.saveUser(user);
        return "User registerted successfully";
    }
}

我完全不了解Spring,请帮我学习如何操作?


请查看:https://dev59.com/E1LTa4cB1Zd3GeqPdcrb - Krzysztof Cichocki
7个回答

214

当使用permitAll时,这意味着每个已验证的用户可以访问该资源,但是您禁用了匿名访问,因此这不起作用。

您想要忽略某些URL,请重写configure方法,该方法接受一个WebSecurity对象和ignore模式参数。

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/api/v1/signup");
}

将该行从HttpSecurity部分删除。这将告诉Spring Security忽略此URL,并不对其应用任何过滤器。


6
这会被写入哪个文件? - Jacob Zimmerman
3
@Web安全配置类的配置器。 - onkami
2
只是想补充一下,您必须扩展WebSecurityConfigurerAdapter并在其中覆盖方法 - muasif80
那么你没有正确配置事物,否则它应该可以工作。 - M. Deinum
1
在Spring Security 5.7.0-M2中,我们已经弃用了WebSecurityConfigurerAdapter,因为我们鼓励用户转向基于组件的安全配置。 - Niamatullah Bakhshi
显示剩余2条评论

30

我有一个更好的方法:

http
    .authorizeRequests()
    .antMatchers("/api/v1/signup/**").permitAll()
    .anyRequest().authenticated()

4
这个片段应该在哪里被调用? - Viacheslav Shalamov
2
@ViacheslavShalamov 在你的 WebSecurityConfig extends WebSecurityConfigurerAdapterconfigure(HttpSecurity http) 方法中。请参阅 https://www.baeldung.com/java-config-spring-security - jAC
11
这在互联网上很常见,但实际上这是错误的做法。如果您允许所有操作,那么您仍需要进行身份验证,最终才能许可该操作。那么为什么我们应该在注册访问时进行身份验证(我的意思是身份验证过滤器仍将被触发)? - Chao
1
@Chao,你有什么推荐? - Leponzo

15
<http pattern="/resources/**" security="none"/>

或者使用Java配置:

web.ignoring().antMatchers("/resources/**");

不再使用旧的方法:

 <intercept-url pattern="/resources/**" filters="none"/>

例如,为登录页面禁用安全性:

  <intercept-url pattern="/login*" filters="none" />

12
这可能不是你问题的完整答案,但如果你想禁用csrf保护,可以这样做:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/web/admin/**").hasAnyRole(ADMIN.toString(), GUEST.toString())
                .anyRequest().permitAll()
                .and()
                .formLogin().loginPage("/web/login").permitAll()
                .and()
                .csrf().ignoringAntMatchers("/contact-email")
                .and()
                .logout().logoutUrl("/web/logout").logoutSuccessUrl("/web/").permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin").password("admin").roles(ADMIN.toString())
                .and()
                .withUser("guest").password("guest").roles(GUEST.toString());
    }

}

我已经包含了完整的配置,但关键行是:

.csrf().ignoringAntMatchers("/contact-email")

7
如@M.Deinum已经写了答案。
我尝试使用api /api/v1/signup,它将绕过过滤器/自定义过滤器,但浏览器会发出一个额外的请求来获取/favicon.ico,所以我也在web.ignoring()中添加了这个,并且对我有用。
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/api/v1/signup", "/favicon.ico");
}

也许对于上述问题来说这并不是必需的。

1

如果您想忽略多个API端点,可以按照以下方式进行:

 @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable().authorizeRequests() 
            .antMatchers("/api/v1/**").authenticated()
            .antMatchers("api/v1/authenticate**").permitAll()
            .antMatchers("**").permitAll()
            .and().exceptionHandling().and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

1
我在这里遇到了同样的问题,以下是解决方案: (已解释)
@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers(HttpMethod.POST,"/form").hasRole("ADMIN")  // Specific api method request based on role.
            .antMatchers("/home","/basic").permitAll()  // permited urls to guest users(without login).
            .anyRequest().authenticated()
            .and()
        .formLogin()       // not specified form page to use default login page of spring security.
            .permitAll()
             .and()
        .logout().deleteCookies("JSESSIONID")  // delete memory of browser after logout.

        .and()
        .rememberMe().key("uniqueAndSecret"); // remember me check box enabled.

    http.csrf().disable();  **// ADD THIS CODE TO DISABLE CSRF IN PROJECT.**
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接