- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
默认情况下,sec:authorize-url 标签不适用于 Spring boot 安全性:
git clone https://github.com/spring-projects/spring-boot
添加依赖
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity3</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
根据示例调整 Controller :
@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";
}
将 URL 匹配添加到应用程序安全性:
http.authorizeRequests().antMatchers("/login").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
...
在 home.html 中添加测试代码
<div sec:authorize="hasRole('ROLE_ADMIN')">
has role admin
</div>
<div sec:authorize-url="/admin/foo">
can see /admin
</div>
当我启动应用程序并登录时,无论我是否可以实际访问该网址,我总是会看到“可以看到/admin”部分。角色评估本身按预期工作,url 权限本身也按预期工作(当我尝试使用 ROLE_USER 访问它时,我收到 403)。
如果我向 Web 安全配置添加一个虚拟特权评估器,该配置仅对每个请求返回 false,则 div 将正确消失。
我在这里遗漏了什么吗?这是预期的行为吗?我需要定义什么才能使authorize-url 按照使用 xml 配置安全性时的方式工作?
此问题与 SpringBootWebSecurityConfiguration 中的基本身份验证及其自动配置有关:
在 SampleMethodSecurityApplication 中,通过替换以下内容来更改 ApplicationSecurity 顺序:
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
与
@Order(SecurityProperties.BASIC_AUTH_ORDER + 1)
并在 Spring Boot 中停用基本 application.properties
security.basic.enabled: false
现在,authorize-url 标签将按预期工作,但当然您已经失去了 http 基本自动配置。
保留 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));
}
});
}
它允许您使用推荐的 ACCESS_OVERRIDE_ORDER 和 http 基本自动配置。我已经发布了更多详细信息here任何解释为什么它有效的解释都是值得赞赏的。
最佳答案
使用thymeleaf-extras-springsecurity4应该可以解决问题
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
关于Spring Boot 安全 - Thymeleaf 秒 :authorize-url not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23686022/
问题:如何对文本文字中的多个连续下划线进行转义? 我正在为 HTML 使用标准的 Thymeleaf 方言(我不在这里使用 Spring 或 SpEL)。 在 Thymeleaf 中,我可以将下划线创
在 SaaS 应用程序中,我使用了一些模板来生成通知电子邮件或某些 HTML 页面。到目前为止,我没有使用 thymeleaf,而且所有模板都是硬编码的,但我很想改变它,以便应用程序的用户可以自己编辑
我看到JSP页面有.jsp/.jspf/.jspx后缀(来自 JavaServer Pages™ Specification Version2.2),Velocity 模板使用 .vm后缀,FreeM
我有一个像这样的 Thymeleaf 片段 ... 脚本部分我只想包含它一次,即使我会在页面中多次包含 f1 。实现这一目标最简单/最干净的方法是什么? 我什至可以将此片段拆
两个 Thymeleaf 属性有什么区别:th:include 和 th:replace? 最佳答案 根据documentation如果您遇到这种情况: content here 片段将被放置在
我是 Thymeleaf 初学者。我从一个通用布局页面开始: fragments/layout.html Template title Some text
我有两个数组,我想在同一个表(不同的列)中显示其内容。如何使用 index 或 th:each 遍历数组 这是我想要实现的目标 List1Elm1 List
我在 session 中有一个对象,例如一个部门,这个部门有 child 。我得到了它的 child 的列表,现在我想在这个列表中添加这个部门对象。这在服务器端非常简单,但可以做到这个在 thymel
我的 Thymeleaf 页面中有几个下拉列表,如下所示: 当我查看页面时,列表中的第一个值显示为已选中,并且实际上已作为选中值提交,即使它不是手动选中的。我宁愿默认不选择任
我有一个通用的布局,默认情况下,除已包含(更高级的)搜索表单的搜索页面本身之外,每个页面上均应显示(基本)搜索表单。 是否可以将参数从我的搜索页面传递到版式,以便不显示默认搜索表单? 这是我想做的一个
我有一个 User 对象列表,我想将它转换为一个名称列表,加入它并呈现它(不是在表格中)。我该怎么做? class User { String name; String address; }
我在前端使用thymeleaf,我知道variable中的thymeleaf概念 如果我使用th:text,变量中的值将被打印,并且我可以在同一元素中使用该变量。有没有办法在其他元素中使用var呢?
我知道 Thymeleaf 是为渲染 View 而制作的,但是我只是想知道是否有任何方法可以在 Thymeleaf 片段的请求范围内设置变量? 我有一个非常大的条件表达式,我必须在整个应用程序中重复很
假设我有两个 Thymeleaf 模板: index.html : foo bar 片段/main.html : This is the main cont
我想声明一些布局用作所有表单字段的模板。 大致给出这个片段 Edition description 这个片段“调用” 它将产生
在 Thymeleaf 中实现 Markdown 的最佳方式是什么? 模板模式 一种新的方言(什么处理器?) 如果我可以在 HTML 中嵌入 markdown,那将会很有用。 最佳答案 根据我对 Ja
我想使用模板片段创建最多包含三个项目的列表。无论是否有项目,项目都会显示三个空格,因此看起来像这样。 0}" th:insert="code-block :: block(${bloc
如何从 Thymeleaf 重定向页面(我有如下 JSP 代码) out.println("REDIRECT=http://www.example.com/api/response?id="+id)
我想在 Thymeleaf 的字符串中放置双引号,我有以下形式: 我想要的结果是: Value of "apple" is "1.5". 但我得到以下异常: EL1065E: unexpected
我想使用模板片段创建最多包含三个项目的列表。无论是否有项目,项目都会显示三个空格,因此看起来像这样。 0}" th:insert="code-block :: block(${bloc
我是一名优秀的程序员,十分优秀!