【发布时间】:2015-09-05 21:20:54
【问题描述】:
我正在尝试使用多个角色配置 spring boot-Embedded Tomcat 基本 HTTP 身份验证,其中大多数 url 相似,但很少有特定于每个角色的。在这里,对于第一个角色,基本的 HTTP 身份验证会弹出并正常工作。使用下面的代码,
@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class TestSecurityAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests().antMatchers(null, getAppAdminRolePaths()).authenticated()
.anyRequest().hasAnyRole("APPADMIN")
.and()
.httpBasic();
http.csrf().disable()
.authorizeRequests().antMatchers(null, getAppUserRolePaths()).authenticated()
.anyRequest().hasAnyRole("APPUSER")
.and()
.httpBasic();
http.authorizeRequests().antMatchers(null, new String[]{"/app/appOwnerView.html"}).authenticated()
.anyRequest().hasAnyRole("APPOWNER")
.and()
.httpBasic();
}
@Override
@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("appadminname").password("appadminpwd").roles("APPADMIN").and()
.withUser("appusername").password("appuserpwd").roles("APPUSER").and()
.withUser("appownername").password("appoownerpwd").roles("APPOWNER");
}
private static String[] getAppAdminRolePaths(){
return new String[]{"/appweb/*",
"/app/checkService.html",
"/app/index.html",
"/app/testData.html",
"/app/adminView.html",
"/app/demo.html"};
}
private static String[] getAppUserRolePaths(){
return new String[]{"/appweb/*",
"/app/checkService.html",
"/app/index.html",
"/app/testData.html",
"/app/userView.html",
"/app/demo.html"};
}
}
对于带有 url http://localhost:8080/app/index.html 的浏览器中的 HTTP 用户名/密码弹出窗口,使用 appadminname/appadminpwd 可以正常工作。但是对于相同的 url,如果我输入 appusername/appuserpwd 它会抛出 HTTP 403 禁止访问错误。我不确定为什么配置的第二个角色 APPUSER 会抛出此错误。请告知是否有办法解决此问题。
谢谢
【问题讨论】:
标签: spring-security spring-boot basic-authentication spring-4 embedded-tomcat-7