[SPRING] 봄 보안 및 Thymeleaf가 작동하지 않음
SPRING봄 보안 및 Thymeleaf가 작동하지 않음
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>
결과로 나는 본다 :
즉 봄 보안이 작동하지 않습니다.
내 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와 함께 작동하지 않는 이유를 이해할 수 없습니다. 도와주세요!
해결법
-
==============================
1.나는 그 문제를 해결했다. 내 설정에 SpringSecurityDialect를 추가하지 않았다. 이제 내 설정은 다음과 같습니다.
나는 그 문제를 해결했다. 내 설정에 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>
잘 작동합니다!
-
==============================
2.
@Bean public SpringTemplateEngine templateEngine(){ SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); templateEngine.setEnableSpringELCompiler(true); // add dialect spring security templateEngine.addDialect(new SpringSecurityDialect()); return templateEngine; }
from https://stackoverflow.com/questions/23348341/spring-security-and-thymeleaf-doesnt-work by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] 스프링 MVC 정적 리소스 매핑 (0) | 2019.05.10 |
---|---|
[SPRING] 포트 80에서 스프링 부트가 시작되지 않음 (0) | 2019.05.10 |
[SPRING] 자바 코드에서 작업을 예약하는 방법 (0) | 2019.05.10 |
[SPRING] 봄 애플 리케이션 개체에 속성을 찾을 수 없습니다 (0) | 2019.05.10 |
[SPRING] 인터페이스의 사용자 정의 주석에 대한 구성 요소 스캔 (0) | 2019.05.10 |