Spring Security hasRole() 不工作

新手上路,请多包涵

我在使用 Spring Security && Thymeleaf 时遇到问题,特别是在尝试使用 hasRole 表达式时。 ‘admin’ 用户具有角色 ‘ADMIN’ 但 hasRole('ADMIN') 无论如何都解析为 false 我试试

我的网页:

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

结果是:

 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

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

 <security:http auto-config="true" use-expressions="true">

并且还在我的配置中包含了 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>

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

角色.java

 @Entity
@Table(name = "roles")

    public class Role implements Serializable {

        @Id
        @Enumerated(EnumType.STRING)
        private RoleType name;
        //... getters, setters
    }

角色类型

public enum RoleType {

    ADMIN
}

并且 User 有一组 Role s

为什么 hasRole() 不工作?

感谢您的帮助,谢谢

解决方法

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

原文由 Xipo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

尝试在 HTML 标签内使用 hasAuthority 代替 hasRole

 sec:authorize="hasAuthority('ADMIN')"

原文由 Dmitry Stolbov 发布,翻译遵循 CC BY-SA 3.0 许可协议

您缺少一个概念:

  • 如果您使用 hasRole('ADMIN') ADMIN 在您的 ADMIN Enum ROLE_ADMIN
  • 如果您使用 hasAuthority('ADMIN') ,您的 ADMIN Enum 必须是 ADMIN

In spring security, hasRole() is the same as hasAuthority() , but hasRole() function map with Authority without ROLE_ prefix .

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

原文由 Huy Quang 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题