【问题标题】:Sec:authorize doesn't work in spring security 4Sec:authorize 在 Spring Security 4 中不起作用
【发布时间】:2018-10-31 06:28:38
【问题描述】:

我正在尝试使用弹簧安全性。我在内存管理员和用户中创建了两个用户。

auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
        auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");

这是我的 pom.xml:

<dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    </dependency>

这是我的html页面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" 
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/myStyle.css"/>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<script type="text/javascript" src="js/myApp.js"></script>
<link rel="stylesheet" href="css/print.css" type="text/css" media="print" />
</head>
<body ng-app="myStockApp">
<div ng-controller="mainctr">
<div data-ng-init="listArticles()"> 

<div sec:authorize="hasRole('ROLE_ADMIN')">Only for admin</div>

//code ...

我希望只有管理员才能看到该 div,但是当我以用户身份登录时,上面的 div 会出现,或者它应该消失。你能告诉我问题在哪里吗?谢谢。

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    搜索并尝试理解问题后,我发现方言有问题

    您可以在此处查看如何添加方言: How do I add Thymeleaf SpringSecurityDialect to spring boot

    tl;博士 将此添加到您的配置中

        @Bean
     public SpringSecurityDialect securityDialect() {
         return new SpringSecurityDialect();
     }        
    

    【讨论】:

      【解决方案2】:

      确保您使用的是 spring security 4,如果您没有将 thymeleaf-extras-springsecurity 更改为正确的

      【讨论】:

        【解决方案3】:

        你能把这个换成百里香吗

        <div sec:authorize="hasRole('ROLE_ADMIN')">Only for admin</div>
        

        以下

        <div sec:authorize="hasAuthority('ADMIN')">Only for admin</div>
        

        如果它适合你,请告诉我。

        【讨论】:

        • 嗨,Periklis,同样的问题仍然存在。
        • 很奇怪,我用的是 ... 但它相当于@PeriklisDouvitsas 的东西
        猜你喜欢
        • 2021-06-20
        • 2016-08-03
        • 2014-05-26
        • 2015-11-26
        • 2020-08-05
        • 2015-12-09
        • 2014-12-12
        • 2021-02-23
        • 2015-04-20
        相关资源
        最近更新 更多