phl*_*bas 5 spring spring-security thymeleaf spring-boot spring-java-config
默认情况下,sec:authorize-url标记不能与Spring启动安全性一起使用:
git clone https://github.com/spring-projects/spring-boot
Run Code Online (Sandbox Code Playgroud)
添加依赖项
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity3</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
调整样本中的控制器:
@RequestMapping("/")
public String home(Map<String, Object> model) {
model.put("message", "Hello World");
model.put("title", "Hello Home");
model.put("date", new Date());
return "home";
}
@RequestMapping("/admin/foo")
public String home2(Map<String, Object> model) {
model.put("message", "Hello World");
model.put("title", "Hello Home");
model.put("date", new Date());
return "home";
}
Run Code Online (Sandbox Code Playgroud)
添加与应用程序安全性匹配的URL:
http.authorizeRequests().antMatchers("/login").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
...
Run Code Online (Sandbox Code Playgroud)
在home.html中添加testcode
<div sec:authorize="hasRole('ROLE_ADMIN')">
has role admin
</div>
<div sec:authorize-url="/admin/foo">
can see /admin
</div>
Run Code Online (Sandbox Code Playgroud)
当我启动应用程序并登录时,无论我是否可以实际访问该URL,我都会看到"可以看到/管理员"部分.角色评估本身按预期工作,url权限本身也是如此(当我尝试使用ROLE_USER访问它时,我得到403).
如果我向web安全配置添加一个dummy privilegeEvaluator,它只是为每个请求返回false,则div将正确消失.
我在这里错过了什么吗?这是预期的行为,我需要定义什么才能使authorize-url以与使用xml配置安全性时相同的方式工作?
此问题与SpringBootWebSecurityConfiguration中的基本身份验证及其自动配置相关联:
在SampleMethodSecurityApplication中,通过替换以下内容来更改ApplicationSecurity顺序:
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
Run Code Online (Sandbox Code Playgroud)
同
@Order(SecurityProperties.BASIC_AUTH_ORDER + 1)
Run Code Online (Sandbox Code Playgroud)
并在spring boot application.properties中停用basic
security.basic.enabled: false
Run Code Online (Sandbox Code Playgroud)
现在,authorize-url标记将按预期工作,但您当然丢失了http basic AutoConfiguration.
保留security.basic.enabled:true并将ApplicationSecurity的顺序更改为高于BASIC_AUTH_ORDER将使您使用基本身份验证而不是表单登录...
我找到了以下解决方法.只需在SecurityConfig中手动注册安全拦截器:
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
public void configure(final WebSecurity web) throws Exception {
final HttpSecurity http = getHttp();
web.postBuildAction(new Runnable() {
@Override
public void run() {
web.securityInterceptor(http.getSharedObject(FilterSecurityInterceptor.class));
}
});
}
Run Code Online (Sandbox Code Playgroud)
它允许您使用推荐的ACCESS_OVERRIDE_ORDER和http基本自动配置.我在这里发布了更多细节 任何解释为什么这个工作表示赞赏.
使用thymeleaf-extras-springsecurity4应该可以解决问题
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4753 次 |
| 最近记录: |