spring-security之生產環境的用戶登錄(查詢數據庫)

spring-security通過認證類查詢數據庫中的表:
spring-security把用戶在前端頁面提交的用戶名傳參給認證類
認證類通過用戶名在數據庫中查詢數據
認證類將查詢結果返回給spring-security,
spring-security將認證類返回的數據和頁面提交的數據進行比對,如果用戶名和密碼匹配就登錄成功,不匹配則登錄失敗

導入依賴信息

<!-- 身份驗證 -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
</dependency>

配置web.xml:

  • 掃描spring-security.xml
  • 添加過濾器
<!-- 配置springSecurity安全過濾器 -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

配置spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/security
		http://www.springframework.org/schema/security/spring-security.xsd">

    <!-- 以下資源不被攔截 -->
    <http pattern="/css/**" security="none"/>
    <http pattern="/img/**" security="none"/>
    <http pattern="/js/**" security="none"/>
    <http pattern="/plugins/**" security="none"/>


    <http pattern="/seller/add.do" security="none"/>
    <http pattern="/*.html" security="none"/>

    <!-- 頁面攔截規則 -->
    <http use-expressions="false">
        <intercept-url pattern="/**" access="ROLE_SELLER" />
        <form-login login-page="/shoplogin.html"
                    default-target-url="/admin/index.html" authentication-failure-url="/shoplogin.html"
                    always-use-default-target="true" />
        <csrf disabled="true" />
        <headers>
            <frame-options policy="SAMEORIGIN" />
        </headers>
        <logout />
    </http>

    <!-- 認證管理器 -->
    <authentication-manager>
        <authentication-provider user-service-ref="userDetailService"/>
    </authentication-manager>

    <!-- 定義自定義認證類 -->
    <beans:bean id="userDetailService" class="com.weilinyang.service.UserDetailsServiceImpl"/>

</beans:beans>

創建自定義認證類UserDetailsServiceImpl,並在spring-security.xml進行配置
自定義認證類要實現spring-security的接口UserDetailService

public class UserDetailsServiceImpl implements UserDetailsService {

    @Reference
    private SellerService sellerService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 構建角色列表
        List<GrantedAuthority> grantAuths = new ArrayList<GrantedAuthority>();
        grantAuths.add(new SimpleGrantedAuthority("ROLE_SELLER"));

        //查詢數據庫的表
        TbSeller seller = sellerService.findOne(username);
        if (seller!=null){
            return new User(username,seller.getPassword(),grantAuths);
        }else{
            return null;
        }

    }
}

seivice層就是根據用戶名查詢用戶

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