【问题标题】:Disable spring security authentication in specific method在特定方法中禁用spring安全认证
【发布时间】:2015-07-15 06:58:49
【问题描述】:

我在我的应用程序上使用 Spring Security 4。在我的应用程序中,我有注册页面,我想将此页面排除在检查身份验证之外。 如何排除特定控制器方法的身份验证?

我的安全配置:

 http.antMatcher("/test")
            .httpBasic()
            .and()
            .authorizeRequests()
            .antMatchers("/index.html", "/login.html", "/",       "/scripts/**",
                    "/bower_components/**", "/styles/**", "/views/**",
                    "/login", "/api/user/*").permitAll().anyRequest()
            .authenticated().and().logout().logoutUrl("/api/logout").and()
            .csrf().csrfTokenRepository(csrfTokenRepository()).and()
            .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);

我的控制器方法:

@RequestMapping(method = RequestMethod.POST, value = "/registration")
public ResponseEntity<RestUser> registration(
        @RequestBody RestUser restuser, Principal p) throws Exception {

}

【问题讨论】:

标签: java spring spring-security


【解决方案1】:

将注册 URL 添加到您的配置中的全部许可。我已将 /registration 添加到您的 Spring 安全配置中

http.antMatcher("/test")
            .httpBasic()
            .and()
            .authorizeRequests()
            .antMatchers("/index.html", "/login.html", "/",       "/scripts/**",
                    "/bower_components/**", "/styles/**", "/views/**",
                    "/login", "/api/user/**","/registration").permitAll().anyRequest()
            .authenticated().and().logout().logoutUrl("/api/logout").and()
            .csrf().csrfTokenRepository(csrfTokenRepository()).and()
            .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);

更新

请将/api/user/* 更改为/api/user/**。它应该可以工作。

【讨论】:

  • “/registration”已经是“/api/user/*”的一部分,这仍然希望我使用浏览器默认身份验证对话框进行身份验证。
  • 注册页面的完整路径是什么样的?
  • /api/user/registration
猜你喜欢
  • 2016-12-12
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
  • 2018-05-19
  • 2018-04-26
  • 2014-10-30
  • 1970-01-01
相关资源
最近更新 更多