spring security注意點

一個Spring Security的數據庫認證實例,要注意以下幾點:
(1)請自行加入Spring必須的包,Spring security的包和MySQL的驅動包,當然你也可以換成其他的數據庫,但是你要相應的修改spring-common.xml中的dataSource部分
(2)users表必須包含username,password,enabled字段,這三個字段是絕對不能少的,也不能修改類型.另外enabled一定要爲1才能登錄,當然可以叫別的字段但是必須as爲這幾個名字。
(4)角色名字必須滿足ROLE_XXX的格式(例如:ROLE_ADMIN,ROLE_USER,ROLE_MAMAGER),當然這是默認情況,也可以通過修改配置文件自己設定一個自定義的開頭。

(5)注意自定義登陸頁面的action名字,以及j_username,j_password。

以上第四條新手尤其注意,困擾了我一天時間。以下附上配置片段。

<authentication-provider>  
        <jdbc-user-service data-source-ref="dataSource"
                           users-by-username-query="select username,password,isvalid as enabled from user where username=?"
                           authorities-by-username-query="select u.username,r.name as authority from user u join role r on u.roleid=r.id where u.username=?"/>
    </authentication-provider>
    
    <beans:bean id="filterSecurityInterceptor"
                class="org.springframework.security.intercept.web.FilterSecurityInterceptor" autowire="byType">
    <custom-filter before="FILTER_SECURITY_INTERCEPTOR"/>
    <beans:property name="objectDefinitionSource" ref="filterInvocationDefinitionSource"/>
    </beans:bean>

    <beans:bean id="filterInvocationDefinitionSource"
                class="cn.javalib.unilines.security.JdbcFilterInvocationDefinitionSourceFactoryBean">
                <beans:property name="dataSource" ref="dataSource"/>
                <beans:property name="resourceQuery" value="select re.url as res_string,r.name from role r join role_resource rr on r.id=rr.role_id join resource re on re.id=rr.resources_id"/>
    </beans:bean>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章