2015-11-02 95 views

回答

1

似乎沒有一種簡單的方法可以忽略普通彈簧安全配置中的前綴。但是,我發現從數據庫加載時,可以輕鬆地將「ROLE_」附加到所有角色。我只是使用MySQL中的CONCAT函數來爲所有角色添加「ROLE_」。請看下圖:

<authentication-manager> 
    <authentication-provider> 
     <password-encoder hash="md5"/> 
     <jdbc-user-service 
      data-source-ref="dataSource" 
      authorities-by-username-query="SELECT username, CONCAT('ROLE_', rolename) as authority FROM user_role WHERE active = 1 AND username = ?" 
      users-by-username-query="SELECT username, password, active FROM user WHERE username = ?" /> 
    </authentication-provider> 
</authentication-manager> 

,如果你使用的UserDetailsS​​ervice接口創建自己的類此概念也可應用於:

<!-- Select users and user_roles from database --> 
<authentication-manager alias="authenticationManager"> 
    <authentication-provider user-service-ref="myUserDetailsService"> 
     <password-encoder ref="encoder" /> 
    </authentication-provider> 
</authentication-manager> 

<beans:bean id="encoder" class="com.my.security.MyPasswordEncoder" /> 

注:我用一個「別名」爲「驗證管理器」,而不是的ID,因爲我的自定義似乎在身份驗證過程中被忽略,如果我重寫默認ID。

相關問題