本文介绍了Spring安全和Thymeleaf不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Spring 4 and ThymeleafIn my index.xhtml page i wrote:

  <!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>

结果我看到:

即。 Spring Security不起作用

i.e. Spring Security doesn't work

我的build.gradle(一些dependecies)是:

My build.gradle (some dependecies) are:

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是:

My spring-security.xml is:

<?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合作。
请帮忙!

Before, I used the Apache Tiles and for it all works well.I don't understand why Spring Secury don't work with Thymeleaf.Please, help!

推荐答案

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

I solved the problem.I just didn't add the SpringSecurityDialect to my config. Now my config looks as

<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>

并且运行正常!

这篇关于Spring安全和Thymeleaf不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 22:19