spring Security的自定義用戶認證過程詳解
更新時間:2019年09月29日 14:57:19 作者:King-D
這篇文章主要介紹了spring Security的自定義用戶認證過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
首先我需要在xml文件中聲明.我要進行自定義用戶的認證類,也就是我要自己從數(shù)據(jù)庫中進行查詢
<http pattern="/*.html" security="none"/> <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"/> <!-- use-expressions:設(shè)置是否啟動SpEL表達式,默認值是true。 --> <http use-expressions="false"> <!-- 配置SpringSecurity的攔截路徑(攔截規(guī)則) * pattern:配置攔截規(guī)則。 /* 代表的是根路徑下的所有資源(不包含子路徑) /**代表的是根路徑下所有的資源(包含子路徑) * access:設(shè)置角色 角色命名 ROLE_角色名稱 如: ROLE_USER --> <intercept-url pattern="/**" access="ROLE_SELLER"/> <!-- 開啟表單驗證 username-parameter="username" password-parameter="password" login-page :登錄頁面名稱 以 / 開始 default-target-url :登錄成功后跳轉(zhuǎn)的頁面 login-processing-url:提交的路徑的設(shè)置 默認值"/login" 可以修改 --> <form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" always-use-default-target="true" authentication-failure-url="/shoplogin.html"/> <!-- 不使用csrf的校驗 --> <csrf disabled="true"/> <!-- 配置框架頁面不攔截 --> <headers> <frame-options policy="SAMEORIGIN"/> </headers> <!-- 注銷的配置 --> <logout logout-url="/logout" logout-success-url="/shoplogin.html" /> </http> <!-- 配置認證管理器 --> <authentication-manager> <!-- 認證的提供者 --> <authentication-provider user-service-ref="userDetailService"> <password-encoder ref="passwordEncoder"></password-encoder> </authentication-provider> </authentication-manager> <!-- 配置自定義的認證類 --> <beans:bean id="userDetailService" class="com.qingmu2.core.service.UserDetailServiceImpl"> <beans:property name="sellerService" ref="sellerService"></beans:property> </beans:bean> <!--加密時候使用的算法是BCryptPasswordEncoder--> <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
配置完自定義的文件以后,在需要自定義認證類的模塊中實現(xiàn)
UserDetailsService
package com.qingmu2.core.service; import com.alibaba.dubbo.config.annotation.Reference; import com.qingmu2.core.pojo.seller.Seller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; /** * 自定義的認證類 * @Auther:qingmu * @Description:腳踏實地,只為出人頭地 * @Date:Created in 8:33 2019/5/31 */ public class UserDetailServiceImpl implements UserDetailsService { private SellerService sellerService; public UserDetailServiceImpl() { } @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Seller seller = sellerService.findOne(username); if(null!=seller){ //判斷一次商家是否被審核通過. if("1".equals(seller.getStatus())){ //創(chuàng)建一個集合,用來存儲權(quán)限 HashSet<GrantedAuthority> authorities = new HashSet<>(); authorities.add(new SimpleGrantedAuthority("ROLE_SELLER")); //將這個用戶的信息返回給認證類 return new User(username,seller.getPassword(),authorities); } } //沒有這個用戶,則返回null return null; } public UserDetailServiceImpl(SellerService sellerService) { this.sellerService = sellerService; } public SellerService getSellerService() { return sellerService; } public void setSellerService(SellerService sellerService) { this.sellerService = sellerService; } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot復雜參數(shù)應(yīng)用詳細講解
我們在編寫接口時會傳入復雜參數(shù),如Map、Model等,這種類似的參數(shù)會有相應(yīng)的參數(shù)解析器進行解析,并且最后會將解析出的值放到request域中,下面我們一起來探析一下其中的原理2022-09-09maven-surefire-plugin總結(jié)示例詳解
這篇文章主要介紹了maven-surefire-plugin總結(jié),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07Java servlet、filter、listener、interceptor之間的區(qū)別和聯(lián)系
這篇文章主要介紹了Java servlet、filter、listener、interceptor之間的區(qū)別和聯(lián)系的相關(guān)資料,需要的朋友可以參考下2016-11-11