/ / PreAuthorized( "hasRole()")は、3.2から4.0.2へのアップグレードスプリングセキュリティの後、常にfalseを返します。 - spring、spring-security

PreAuthorized( "hasRole()")は、3.2から4.0.2へのアップグレードスプリングセキュリティ後に常にfalseを返します。 - spring、spring-security

私は春のセキュリティをバージョン4.0にアップグレードした後。2バージョン3.2.xから、私はWebアプリケーションにログインすることができますが、@ PreAuthorized(hasRole())を持つメソッドにアクセスしようとするとアクセスが拒否されます。

私はGrantAuthoritiesリストに "ROLE_"を注入しようとしましたが、結果は同じです。

同じ構成がバージョン3.2.xで正常に機能します。私は間違って何をしたのですか?

ありがとう

security-context.xml

<http pattern="/css/**" security="none" />
<http pattern="/Images/**" security="none" />
<http pattern="/javascript/**" security="none" />

<http auto-config="true" use-expressions="true" create-session="always"
authentication-manager-ref="tunAuthenticationManager">
<csrf disabled="true" />

<intercept-url pattern="/new/**"
access="hasRole("eu_rw") or hasRole("sp_rw")" />
<intercept-url pattern="/ajax/**"
access="hasRole("eu_rw") or hasRole("sp_rw")" />
<intercept-url pattern="/v2/**"
access="hasRole("eu_rw") or hasRole("sp_rw")" />

<intercept-url pattern="/monitoring" access="hasRole("sp_rw")" />
<intercept-url pattern="/monitoring/**" access="hasRole("sp_rw")" />

<form-login login-page="/logon.jsp" username-parameter="username"
password-parameter="password" login-processing-url="/j_spring_security_check"
authentication-failure-url="/logon.jsp?login_error=1"
default-target-url="/" always-use-default-target="true" />

<custom-filter position="FIRST" ref="logoutFilter" />
<custom-filter after="FIRST" ref="requestLoggingFilter" />
<custom-filter after="LAST" ref="passwordExpirationCheckFilter" />
<custom-filter after="SWITCH_USER_FILTER" ref="authorizationAdjustmentFilter" />
<custom-filter after="EXCEPTION_TRANSLATION_FILTER" ref="ajaxTimeoutRedirectFilter" />
</http>



<authentication-manager id="tunAuthenticationManager">
<authentication-provider ref="strongDaoAuthenticationProviderProxy" />
<authentication-provider ref="tunAdAuthenticationProvider" />
</authentication-manager>

<beans:bean id="strongDaoAuthenticationProviderProxy"
class="local.company.tun.security.DaoAuthenticationProviderProxy">
<beans:constructor-arg>
<beans:bean id="strongDaoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="jdbcUserDetailsService" />
<beans:property name="passwordEncoder" ref="strongEncoder" />
<beans:property name="saltSource" ref="saltSource" />
</beans:bean>
</beans:constructor-arg>
</beans:bean>
<beans:bean id="jdbcUserDetailsService"
class="local.company.tun.security.tunUserDetailsService">
</beans:bean>
<beans:bean id="authenticationService"
class="local.company.tun.security.service.impl.AuthenticationServiceImpl">
<beans:qualifier value="authenticationService" />
<beans:property name="authenticationManager" ref="tunAuthenticationManager" />
</beans:bean>
<beans:bean
class="local.company.tun.security.DefaultRolesPrefixPostProcessor" />

servlet-context.xml

<mvc:interceptors>
<!-- Changes the locale when a "locale" request parameter is sent;
e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>

<!-- Activate scanning of @Autowired -->
<context:annotation-config />
<!-- Spring Security - enable pre- post- annotations on Spring managed
MVC components -->
<bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler" >

</bean>
<security:global-method-security pre-post-annotations="enabled">
<security:expression-handler ref="expressionHandler" />
</security:global-method-security>

....

回答:

回答№1は1

あなたは使用する必要があります ROLE_ ロール名の接頭辞と GrantedAuthorities それ以外の場合は、接頭辞を空にしない限り、それはうまくいきません。

RoleVoter.setRolePrefix("");


@PreAuthorize("hasRole("ROLE_ADMIN")")

new SimpleGrantedAuthority("ROLE_ADMIN")

また、デバッグする方法もあります。 userDetails.getAuthorities()メソッドにブレークポイントを設定します。次に、保護されたリソースにアクセスします。 Springは当局の実際のバージョンをチェックし、捕捉されます。スタックトレースを調べ、何がうまくいかないかを比較する場所と値を見つけます。


回答№2の場合は1

問題を解決するには、

<beans:bean
class="local.company.tun.security.DefaultRolesPrefixPostProcessor" />

security-context.xmlとサーブレット・コンテキスト。xml。それは両方の場所に必要なように見えます。 security-context.xml内のBeanは、 "ROLE_"を servlet-context.xmlのBeanは、 "ROLE_"を メソッドレベル。

参照のために、DefaultRolePrefixPostProcessor.javaは、春のセキュリティ移行からコピーされています。3 - > 4.ここでは、完全性のためのソースコードを示します。

パブリッククラスDefaultRolesPrefixPostProcessorは、BeanPostProcessor、PriorityOrdered {

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {

Logger logger = LoggerFactory.getLogger(DefaultRolesPrefixPostProcessor.class);
// remove this if you are not using JSR-250
if(bean instanceof Jsr250MethodSecurityMetadataSource) {
((Jsr250MethodSecurityMetadataSource) bean).setDefaultRolePrefix(null);
}

if(bean instanceof DefaultMethodSecurityExpressionHandler) {
logger.info("overriding the default Role prefix. set it to null ");
((DefaultMethodSecurityExpressionHandler) bean).setDefaultRolePrefix(null);
}
if(bean instanceof DefaultWebSecurityExpressionHandler) {
((DefaultWebSecurityExpressionHandler) bean).setDefaultRolePrefix(null);
}
if(bean instanceof SecurityContextHolderAwareRequestFilter) {
((SecurityContextHolderAwareRequestFilter)bean).setRolePrefix("");
}
return bean;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}

@Override
public int getOrder() {
return PriorityOrdered.HIGHEST_PRECEDENCE;
}

}