簡體   English   中英

OAuth2 JWT授權-春季安全性XML配置#oauth2.hasScope的問題

[英]OAuth2 JWT Authorization - Issue With Spring Security XML Configuration #oauth2.hasScope

我需要在Spring Security實施的某些部分中使用XML配置。 目前我所關心的只是JWT授權,JWT已傳遞給我。 使用Spring Security,我確定用戶是否被授權訪問REST API端點。 我不能使用Java配置或@PreAuthorize批注。

作為FYI,當我最初使用@PreAuthorize或類似方法:.antMatchers(“ / students / **”)。access(“#oauth2.hasScope('Scope:Admin')”);

一切正常。 當我被迫轉移到XML配置並使用security:intercept-url方法時,就出現了這個問題。

我得到的錯誤是:

“ message”:“無法評估表達式'#oauth2.hasScope('Scope:Admin')'”

例外是:

java.lang.IllegalArgumentException:無法評估表達式'#oauth2.hasScope('Scope:Admin')'
在org.springframework.security.access.expression.ExpressionUtils.evaluateAsBoolean(ExpressionUtils.java:30)〜[spring-security-core-5.0.3.RELEASE.jar:5.0.3.RELEASE]
為簡便起見,刪除異常噴出
由以下原因引起:org.springframework.expression.spel.SpelEvaluationException:EL1011E:方法調用:嘗試在空上下文對象上調用方法hasScope(java.lang.String)

XML配置:

<!-- only enable this when deving -->
<!-- <security:debug /> -->  


<bean id="securityConfig"
    class="com.wmay.config.SecurityConfig">
</bean>

<bean id="resourceServerConfig"
    class="com.wmay.config.ResourceServerConfig">
</bean> 


 <bean id="methodSecurityConfig"
    class="com.wmay.config.MethodSecurityConfig">
</bean>

<security:http pattern="/**" use-expressions="true" auto-config="true">
    <security:intercept-url pattern="/students/**"
            access="#oauth2.hasScope('Scope:Admin')"/>
</security:http>

代碼段:

@Override 
public void configure(HttpSecurity httpSecurity) throws Exception {     log.info("Configuring HttpSecurity");       
    httpSecurity.csrf().disable();  httpSecurity.cors().configurationSource(corsConfigurationSource());     
    //@// @formatter:off    
    httpSecurity
                .requestMatchers()
                .and().authorizeRequests()
                .antMatchers("/actuator/**").permitAll()
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll();    
    // @formatter:on 
    }

@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
    log.info(
            "Enabling OAuth2 Method Expression Handler.");
    return new OAuth2MethodSecurityExpressionHandler();
}

我想到了。 我需要在XML配置文件中添加更多條目。

<bean id="oauth2AuthenticationEntryPoint" 
    class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint" />
<oauth2:resource-server id="resourceServerFilter" 
        resource-id="${security.jwt.resource-ids}" token-services-ref="tokenServices"/>
<oauth2:web-expression-handler id="oauth2WebExpressionHandler" />


<security:http 
        pattern="/**"       
        entry-point-ref="oauth2AuthenticationEntryPoint"        
        authentication-manager-ref="authenticationManager"
        use-expressions="true"
        create-session="stateless">
        <security:intercept-url pattern="/students/**"
            access="#oauth2.hasScope('Scope:Admin')" />     
        <security:custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
        <security:expression-handler ref="oauth2WebExpressionHandler" />
    </security:http>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM