- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- springSecurity如何自定義用戶(hù)認證
這篇文章將為大家詳細講解有關(guān)springSecurity如何自定義用戶(hù)認證,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
首先我需要在xml文件中聲明.我要進(jìn)行自定義用戶(hù)的認證類(lèi),也就是我要自己從數據庫中進(jìn)行查詢(xún)
<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:設置是否啟動(dòng)SpEL表達式,默認值是true。 --> <http use-expressions="false"> <!-- 配置SpringSecurity的攔截路徑(攔截規則) * pattern:配置攔截規則。 /* 代表的是根路徑下的所有資源(不包含子路徑) /**代表的是根路徑下所有的資源(包含子路徑) * access:設置角色 角色命名 ROLE_角色名稱(chēng) 如: ROLE_USER --> <intercept-url pattern="/**" access="ROLE_SELLER"/> <!-- 開(kāi)啟表單驗證 username-parameter="username" password-parameter="password" login-page :登錄頁(yè)面名稱(chēng) 以 / 開(kāi)始 default-target-url :登錄成功后跳轉的頁(yè)面 login-processing-url:提交的路徑的設置 默認值"/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"/> <!-- 配置框架頁(yè)面不攔截 --> <headers> <frame-options policy="SAMEORIGIN"/> </headers> <!-- 注銷(xiāo)的配置 --> <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><!-- 配置自定義的認證類(lèi) --> <beans:bean id="userDetailService" class="com.qingmu2.core.service.UserDetailServiceImpl"> <beans:property name="sellerService" ref="sellerService"></beans:property> </beans:bean><!--加密時(shí)候使用的算法是BCryptPasswordEncoder--> <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
配置完自定義的文件以后,在需要自定義認證類(lèi)的模塊中實(shí)現
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;/** * 自定義的認證類(lèi) * @Auther:qingmu * @Description:腳踏實(shí)地,只為出人頭地 * @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){ //判斷一次商家是否被審核通過(guò). if("1".equals(seller.getStatus())){ //創(chuàng )建一個(gè)集合,用來(lái)存儲權限 HashSet<GrantedAuthority> authorities = new HashSet<>(); authorities.add(new SimpleGrantedAuthority("ROLE_SELLER")); //將這個(gè)用戶(hù)的信息返回給認證類(lèi) return new User(username,seller.getPassword(),authorities); } } //沒(méi)有這個(gè)用戶(hù),則返回null return null; } public UserDetailServiceImpl(SellerService sellerService) { this.sellerService = sellerService; } public SellerService getSellerService() { return sellerService; } public void setSellerService(SellerService sellerService) { this.sellerService = sellerService; }}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站