我正在尝试在基于 spring-boot spring MVC 的应用程序中设置单个路径(/basic)以进行基本身份验证保护。我将使用我自己的自定义配置参数进行配置,因此用户名和密码只是“admin”和“admin”。
目前这适用于 /basic 路径(系统会提示我并且可以正确登录)。问题是注销不起作用(我不确定为什么),并且其他路径(如 /other 显示)也被要求提供基本身份验证凭据(在总是被拒绝之前)。
static class MyApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/open").permitAll();
http.authorizeRequests().antMatchers("/other").denyAll(); // Block it for now
http.authorizeRequests().antMatchers("/basic").authenticated().and().httpBasic().and().logout().logoutUrl("/basic/logout").invalidateHttpSession(true).logoutSuccessUrl("/");
}
}
我预计 /other 总是会被拒绝,但我不明白为什么会出现基本身份验证。 /open 按预期工作。我也不明白为什么 /basic/logout 不让我退出(它也不会产生错误消息)。我确实有一段简单的代码作为注销端点的占位符,但如果我没有,则会收到 404。“主页”视图是我的 Web 应用程序根目录,因此我只想在注销后将用户发送到那里。
@RequestMapping("/logout")
public ModelAndView logout() {
// should be handled by spring security
return new ModelAndView("home");
}
更新:
这是最终似乎有效的解决方案(除了注销部分,仍然无效):
@Configuration
@Order(1) // HIGHEST
public static class OAuthSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/oauth").authorizeRequests().anyRequest().denyAll();
}
}
@Configuration
public static class BasicAuthConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/basic").authorizeRequests().anyRequest().authenticated().and().httpBasic();
http.logout().permitAll().logoutUrl("/logout").logoutSuccessUrl("/").invalidateHttpSession(true);
//.and().logout().logoutUrl("/basic/logout").invalidateHttpSession(true).logoutSuccessUrl("/");
}
}
我不确定注销的情况,但我们也遇到了类似的问题,我们的一些网站处于基本状态,而另一些则没有。我们的解决方案是仅对需要 http basic 的路径使用第二个嵌套配置类。我们给这个配置一个@Order(1)..但我不确定这是否有必要。
更新了代码
@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {
private static final Logger LOG = LoggerFactory.getLogger(SecurityConfig.class);
@Autowired
public void registerAuthentication(AuthenticationManagerBuilder auth, Config appConfig) throws Exception {
auth.inMemoryAuthentication()
.withUser(appConfig.getString(APIConfig.CONFIG_KEY_MANAGEMENT_USER_NAME))
.password(appConfig.getString(APIConfig.CONFIG_KEY_MANAGEMENT_USER_PASS))
.roles(HyperAPIRoles.DEFAULT, HyperAPIRoles.ADMIN);
}
/**
* Following Multiple HttpSecurity approach:
* http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#multiple-httpsecurity
*/
@Configuration
@Order(1)
public static class ManagerEndpointsSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/management/**").authorizeRequests().anyRequest().hasRole(HyperAPIRoles.ADMIN).and()
.httpBasic();
}
}
/**
* Following Multiple HttpSecurity approach:
* http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#multiple-httpsecurity
*/
@Configuration
public static class ResourceEndpointsSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
//fyi: This adds it to the spring security proxy filter chain
.addFilterBefore(createBBAuthenticationFilter(), BasicAuthenticationFilter.class)
;
}
}
}
这似乎通过基本身份验证来保护 /management 处的执行器端点,而其他端点则使用自定义身份验证令牌标头。尽管对于任何事情,我们都不会提示提供凭据(不会发出质询)。我们必须注册一些其他内容才能实现这一点(如果我们想要的话)。
希望这可以帮助
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)