web-dev-qa-db-ja.com

Spring Security hasRole()が機能しない

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

結果:

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

Role.Java

_@Entity
@Table(name = "roles")

    public class Role implements Serializable {

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

RoleType

_public enum RoleType {

    ADMIN 
}
_

そして、UserにはRolesのセットがあります

hasRole()が機能しないのはなぜですか?

あなたの助けに感謝します、ありがとう

回避策

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

34
Xipo

HTMLタグ内でhasAuthorityの代わりにhasRoleを使用してみてください。

sec:authorize="hasAuthority('ADMIN')"
73
Dmitry Stolbov

Spring Security 3.xから4.xへのアップグレードでも同じ問題が発生しました。 hasRole()hasAuthority()に変更すると、うまくいきました。

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

13
Jeffrey B.

概念がありません:

  • hasRole('ADMIN')を使用する場合、_ADMIN Enum_はADMINではなく_ROLE_ADMIN_でなければなりません。
  • hasAuthority('ADMIN')を使用する場合、_ADMIN Enum_はADMINでなければなりません。

春のセキュリティでは、hasRole()hasAuthority()と同じですが、hasRole()関数は、Authorityに_ROLE__プレフィックスなしでマップします。

あなたはこの投稿で受け入れられた答えを見つけることができます: Spring SecurityのロールとGrantedAuthorityの違い

7
Huy Quang

ユーザーロールを確認する必要がある場合は、同様のことをしなければなりませんでした。私は以下でした

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

それが誰かを助けることを願っています。

6
rohtakdev

私は最近同じ問題を抱えています。あなたがする必要があるのは:

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

(使用しているものに応じて、springsecurity4またはspringsecurity3の間で変更できます)。

  1. このリソースをライブラリに追加したことを確認してください。私はgradleを使用していますが、Mavenでも同じことができます。

    compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4:2.1.2.RELEASE'
    
  2. SpringWebConfigurationクラスまたはxmlで、thymeleaf SpringSecurityの方言を追加していることを確認してください。構成にJavaクラスを使用しています。

    @Bean
    public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new SpringSecurityDialect());
    return templateEngine;
    }
    

しかし、alexsourceが言うように定義することもできます: 春のセキュリティとThymeleafは機能しません

これがお役に立てば幸いです!ご挨拶!

4
sirandy

公式ドキュメントを参照してください。 http://www.thymeleaf.org/doc/articles/springsecurity.html

<div sec:authorize="hasRole('ROLE_ADMIN')">
  This content is only shown to administrators.
</div>
<div sec:authorize="hasRole('ROLE_USER')">
  This content is only shown to users.
</div>

${ ... }なしで、以下のように試してみてください。

<div sec:authorize="hasRole('ADMIN')">IS ADMIN</div>

ロールの前にROLE_を付けていないと思います。もしそうなら、以下のようにプレフィックスを追加することを確認します

<div sec:authorize="hasRole('ROLE_ADMIN')">IS ADMIN</div>
1
Faraj Farook

spring-security4.0が原因で同じ問題が発生しました。何らかの理由により、thymeleaf-extras-springsecurity4spring-security 4.0およびthymeleaf2.xと互換性がありません。それで、spring-securityバージョンを3.2.9.RELEASEにダウングレードし、機能し始めました。それでもスプリングセキュリティ4.0を使用する場合は、thymeleaf-extras-springsecurity43.0.0.RELEASEに、thymeleaf verisonを3.0に持ち上げることができます。

または、スプリングブートアプリを使用している場合、状況はより複雑になります。残っている唯一のオプションは、spring-securityをダウングレードするか、スプリングブートバージョンを1.4.x(ベータ版のまま)にアップグレードすることです。

特定のシナリオでは、以下のPOMを変更するとhasRoleが機能するはずです

<!--Spring security--> 
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>3.2.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>3.2.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>3.2.9.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>
1