Spring Security hasRole()不起作用

Xip*_*ipo 35 java spring spring-mvc spring-security thymeleaf

我在使用Spring Security && Thymeleaf时遇到问题,特别是在尝试使用hasRole表达式时.'admin'用户有一个'ADMIN'角色,但hasRole('ADMIN')无论如何我会尝试将其解析为false

我的HTML:

1.<div sec:authentication="name"></div> <!-- works fine -->
2.<div sec:authentication="principal.authorities"></div> <!-- works fine -->

3.<div  sec:authorize="isAuthenticated()" >true</div> <!-- works fine -->
4.<span th:text="${#authorization.expression('isAuthenticated()')}"></span> <!-- works fine -->

5.<div th:text="${#vars.role_admin}"></div> <!--Works fine -->
6.<div  sec:authorize="${hasRole('ADMIN')}" > IS ADMIN </div> <!-- Doesnt work -->
7.<div  sec:authorize="${hasRole(#vars.role_admin)}" > IS ADMIN </div> <!-- Doesnt work -->
8.<div th:text="${#authorization.expression('hasRole(''ADMIN'')')} "></div> <!-- Doesnt work -->
9.<div th:text="${#authorization.expression('hasRole(#vars.role_admin)')}"></div> <!-- Doesnt work -->
Run Code Online (Sandbox Code Playgroud)

结果是:

1.admin
2.[ADMIN]
3.true
4.true
5.ADMIN
6."prints nothing because hasRole('ADMIN') resolves to false"
7."prints nothing because hasRole(#vars.role_admin) resolves to false"
8.false
9.false
Run Code Online (Sandbox Code Playgroud)

我在security.xml文件中启用了use-expressions

<security:http auto-config="true" use-expressions="true">
Run Code Online (Sandbox Code Playgroud)

并且还在我的配置中包含了SpringSecurityDialect

<bean id="templateEngine"
      class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver" />  
    <property name="additionalDialects">
        <set>
            <bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect" />
        </set>
    </property>      
</bean>
Run Code Online (Sandbox Code Playgroud)

我的pom.xml文件中的所有必需依赖项

<!--Spring security--> 
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>4.0.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.0.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.0.1.RELEASE</version>
    </dependency>        

    <!--Thymeleaf Spring Security-->
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>2.1.2.RELEASE</version>
        <scope>compile</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

Role.java

@Entity
@Table(name = "roles")

    public class Role implements Serializable {

        @Id
        @Enumerated(EnumType.STRING)
        private RoleType name;
        //... getters, setters
    }
Run Code Online (Sandbox Code Playgroud)

角色类型

public enum RoleType {

    ADMIN 
}
Run Code Online (Sandbox Code Playgroud)

User有一套Roles

为什么hasRole()不工作?

感谢您的帮助,谢谢

解决方法

th:if="${#strings.contains(#authentication.principal.authorities,'ADMIN')}"

Dmi*_*bov 74

尝试使用hasAuthority,而不是hasRoleHTML标签内.

sec:authorize="hasAuthority('ADMIN')"
Run Code Online (Sandbox Code Playgroud)

  • 我遇到过同样的问题.从Spring 3移动到Spring 4时,hasRole()似乎不起作用.使用hasAuthority()为我工作.@Xipo你能否确认它是否适合你并验证这个答案? (5认同)
  • 有一个解释http://stackoverflow.com/questions/19525380/difference-between-role-and-grantedauthority-in-spring-security (4认同)

小智 13

我有同样的问题从Spring Security 3.x升级到4.x. 更改hasRole()hasAuthority()的伎俩我.

http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#el-common-built-in


小智 8

你错过了一个概念:

  • 如果你使用hasRole('ADMIN'),你ADMIN Enum必须ROLE_ADMIN代替ADMIN.
  • 如果你使用hasAuthority('ADMIN'),你的ADMIN Enum必须ADMIN.

在spring security中,hasRole()是一样的hasAuthority(),但hasRole()功能映射Authority没有ROLE_前缀.

您可以在这篇文章中找到接受的答案:Spring Security中Role和GrantedAuthority之间的区别


roh*_*dev 6

我必须做一些类似的事情,我需要验证用户角色.我在下面做了

<div th:if="${ #authorization.expression('isAuthenticated()') and #strings.contains(#authentication.principal.authorities,'ADMIN')}">          
    <a th:href="@{/somelink}">ADMIN LINK</a>
 </div>
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助某人.


sir*_*ndy 6

我最近也遇到了同样的问题。你需要做的是:

  1. 在您的 html 中添加以下语句:

    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"   xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
    
    Run Code Online (Sandbox Code Playgroud)

(您可以根据您使用的内容在 springsecurity4 或 springsecurity3 之间进行更改)。

  1. 确保您已将此资源添加到您的库中。我使用的是 gradle,但你也可以使用 Maven 做同样的事情。

    compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4:2.1.2.RELEASE'
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在您的 SpringWebConfiguration 类或 xml 中,请确保添加 thymeleaf SpringSecurity 的方言:我使用 java 类进行配置。

    @Bean
    public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new SpringSecurityDialect());
    return templateEngine;
    }
    
    Run Code Online (Sandbox Code Playgroud)

但你也可以像 alexsource 所说的那样定义: Spring security 和 Thymeleaf 不起作用

我希望这对你有用!问候!