spring Security的自定義用戶認(rèn)證過程詳解
更新時間:2019年09月29日 14:57:19 作者:King-D
這篇文章主要介紹了spring Security的自定義用戶認(rèn)證過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
首先我需要在xml文件中聲明.我要進(jìn)行自定義用戶的認(rèn)證類,也就是我要自己從數(shù)據(jù)庫中進(jì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:設(shè)置是否啟動SpEL表達(dá)式,默認(rèn)值是true。 -->
<http use-expressions="false">
<!--
配置SpringSecurity的攔截路徑(攔截規(guī)則)
* pattern:配置攔截規(guī)則。 /* 代表的是根路徑下的所有資源(不包含子路徑) /**代表的是根路徑下所有的資源(包含子路徑)
* access:設(shè)置角色 角色命名 ROLE_角色名稱 如: ROLE_USER
-->
<intercept-url pattern="/**" access="ROLE_SELLER"/>
<!--
開啟表單驗(yàn)證
username-parameter="username"
password-parameter="password"
login-page :登錄頁面名稱 以 / 開始
default-target-url :登錄成功后跳轉(zhuǎn)的頁面
login-processing-url:提交的路徑的設(shè)置 默認(rèn)值"/login" 可以修改
-->
<form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" always-use-default-target="true" authentication-failure-url="/shoplogin.html"/>
<!-- 不使用csrf的校驗(yàn) -->
<csrf disabled="true"/>
<!-- 配置框架頁面不攔截 -->
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
<!-- 注銷的配置 -->
<logout logout-url="/logout" logout-success-url="/shoplogin.html" />
</http>
<!-- 配置認(rèn)證管理器 -->
<authentication-manager>
<!-- 認(rèn)證的提供者 -->
<authentication-provider user-service-ref="userDetailService">
<password-encoder ref="passwordEncoder"></password-encoder>
</authentication-provider>
</authentication-manager>
<!-- 配置自定義的認(rèn)證類 -->
<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"/>
配置完自定義的文件以后,在需要自定義認(rèn)證類的模塊中實(shí)現(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;
/**
* 自定義的認(rèn)證類
* @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){
//判斷一次商家是否被審核通過.
if("1".equals(seller.getStatus())){
//創(chuàng)建一個集合,用來存儲權(quán)限
HashSet<GrantedAuthority> authorities = new HashSet<>();
authorities.add(new SimpleGrantedAuthority("ROLE_SELLER"));
//將這個用戶的信息返回給認(rèn)證類
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PostMan傳@RequestParam修飾的數(shù)組方式
這篇文章主要介紹了PostMan傳@RequestParam修飾的數(shù)組方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
java使用歸并刪除法刪除二叉樹中節(jié)點(diǎn)的方法
這篇文章主要介紹了java使用歸并刪除法刪除二叉樹中節(jié)點(diǎn)的方法,實(shí)例分析了java二叉樹算法的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05
Java實(shí)現(xiàn)將CSV轉(zhuǎn)為Excel的示例代碼
CSV(Comma?Separated?Values)文件是一種純文本文件,包含用逗號分隔的數(shù)據(jù),常用于將數(shù)據(jù)從一個應(yīng)用程序?qū)牖驅(qū)С龅搅硪粋€應(yīng)用程序。本文將利用Java實(shí)現(xiàn)CSV轉(zhuǎn)為Excel,感興趣的可以了解一下2022-03-03
Spring mvc工作原理_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Spring mvc工作原理的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08

