gpt4 book ai didi

java - Spring 安全和 Thymeleaf 不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 01:52:21 31 4
gpt4 key购买 nike

我正在使用 Spring 4 和 Thymeleaf在我的 index.xhtml 页面中,我写道:

  <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
layout:decorator="layouts/layout">

<head>
<title>Welcome</title>
</head>
<body>
....
<div sec:authorize="hasRole('ROLE_ADMIN')">
You are authorized user! Hi, <span sec:authentication="name">Username</span>
</div>
<div sec:authorize="isAnonymous()">
You are NOT authorized user!
</div>
...
</body></html>

结果我看到:

You are authorized user! Hi, Username You are NOT authorized user!

即Spring Security 不起作用

我的 build.gradle(一些依赖项)是:

compile 'org.thymeleaf:thymeleaf-spring4:2.1.2.RELEASE'
compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity3:2.1.1.RELEASE'
compile 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:1.2.3'
compile 'org.springframework.security:spring-security-core:3.2.0.RELEASE'
compile 'org.springframework.security:spring-security-web:3.2.0.RELEASE'
compile 'org.springframework.security:spring-security-config:3.2.0.RELEASE'
compile 'org.springframework.security:spring-security-taglibs:3.2.0.RELEASE'

我的 spring-security.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Настройка хранилища безопасности -->
<authentication-manager>
<authentication-provider>
<password-encoder ref="bCryptPasswordEncoder">
</password-encoder>
<jdbc-user-service id="jdbcUserService" data-source-ref="dataSource"
users-by-username-query="select login, password, is_enabled from users where login = ?"
authorities-by-username-query="select u.login, p.`name`
from user_group_ref ug, permission_group_ref pg, users u, groups g, permissions p
where ug.user_id=u.id and ug.group_id=g.id and pg.group_id=g.id and pg.permission_id = p.id and u.login = ?"
group-authorities-by-username-query="select g.id, g.`name`, p.`name`
from user_group_ref ug, permission_group_ref pg, users u, groups g, permissions p
where ug.user_id=u.id and ug.group_id=g.id and pg.group_id=g.id and pg.permission_id = p.id and u.login = ?"
/>
</authentication-provider>
</authentication-manager>

<http use-expressions="true">
<!-- URLs на которых сработает интерцептор безопасности (permitAll - разрешить вход всем (в т.ч. анонимным)-->
<intercept-url pattern="/*" access='permitAll'/>

<!-- Настройка входа пользователя -->
<form-login login-page="/account/signin" authentication-failure-url="/account/login/fail"
username-parameter="login"
password-parameter="password"/>

<!-- Настройка выхода пользователя -->
<logout logout-url="/account/logout" />

<!-- Включает поддержку функции "Запомнить меня" -->
<remember-me remember-me-parameter="remember_me" user-service-ref="jdbcUserService"/>
</http>

<!-- Если указать этот файл в authentication-provider выше, то юзеры будут храниться в этом файле -->
<!-- <user-service id="userService"> -->
<!-- <user name="alexssource" authorities="ROLE_USER" password="123" /> -->
<!-- </user-service> -->

<!-- Хеширование паролей -->
<!--
При создании юзера используется так:
-> PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
-> String encodedPassword = passwordEncoder.encode(password);
-->
<beans:bean id='bCryptPasswordEncoder' class='org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder'/>
</beans:beans>

之前,我使用 Apache Tiles 并且一切正常。我不明白为什么 Spring Secury 不能与 Thymeleaf 一起使用。请帮忙!

最佳答案

我解决了这个问题。我只是没有将 SpringSecurityDialect 添加到我的配置中。现在我的配置看起来像

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> 
<property name="templateResolver" ref="thymeleafResolver" />
<property name="additionalDialects">
<set>
<bean class="nz.net.ultraq.thymeleaf.LayoutDialect" />
<bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
</set>
</property>
</bean>

并且工作正常!

关于java - Spring 安全和 Thymeleaf 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23348341/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com