基于SpringBoot實(shí)現(xiàn)用戶身份驗(yàn)證工具
session失效時間
在Tomcat上,session的默認(rèn)有效時間是30分鐘。也可以通過配置文件修改session的有效時間。
1)修改web.xml
<!-- 設(shè)置session失效,單位分 --> <session-config> <session-timeout>1</session-timeout> </session-config>
2).yml文件
server.session.cookie.http-only= #是否開啟HttpOnly server.session.timeout = #會話超時(秒)
使用過濾器獲取session進(jìn)行身份驗(yàn)證(未全部測試,慎用)
1)新建Filter
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
@ServletComponentScan//讓@WebFilter起作用
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter{
@Autowired
private SessionKeyConfigProperties sessionKeyConfigProperties;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
System.out.println(sessionKeyConfigProperties.getUserTypeKey());
//通過session獲取身份信息
AuthenticationUtil authenticationUtil = new AuthenticationUtil(sessionKeyConfigProperties);
UserTypeEnum userType = authenticationUtil.getUserAuthentication(httpServletRequest.getSession());
//進(jìn)行認(rèn)證
//認(rèn)證失敗
if(userType == null){
//...
}
//用戶不是管理員
if(userType != UserTypeEnum.ADMIN){
//...
}
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
細(xì)心的讀者會發(fā)現(xiàn)我用了AuthenticationUtil,這是為了將讀寫用戶身份認(rèn)證信息的功能分離而設(shè)計(jì)的工具類 2)AuthenticationUtil類
import org.apache.shiro.web.session.HttpServletSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class AuthenticationUtil {
private SessionKeyConfigProperties configProperties;
public AuthenticationUtil(SessionKeyConfigProperties configProperties) {
this.configProperties = configProperties;
}
/**
* 從session中獲取用戶的身份類型
* @param session
* @return 身份類型
*/
public UserTypeEnum getUserAuthentication(HttpSession session){
//獲取session中的用戶信息記錄
Object userType = session.getAttribute(configProperties.getUserTypeKey());
//獲取session中記錄的用戶類型
if(userType != null && userType instanceof UserTypeEnum) {
return (UserTypeEnum)userType;
}
return null;
}
/**
* 將用戶的身份寫入session中
* @param session
* @param userType
*/
public void setUserAuthentication(HttpSession session,UserTypeEnum userType){
session.setAttribute(configProperties.getUserTypeKey(),userType);
}
}
3)配置文件SessiionKeyConfig.properties
user_type_key = userTypeKey
4)配置讀取文件SessionKeyConfigProperties.class
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Configuration
@PropertySource("classpath:config/SessiionKeyConfig.properties")
@Component
public class SessionKeyConfigProperties {
@Value("${user_type_key}")
private String userTypeKey;
public String getUserTypeKey() {
return userTypeKey;
}
public void setUserTypeKey(String userTypeKey) {
this.userTypeKey = userTypeKey;
}
}
5)Enum類
public enum UserTypeEnum {
ADMIN,
USER
}
注:本文刪除了一些package信息及部分import信息。Enum類和配置類的內(nèi)容請根據(jù)項(xiàng)目需求及數(shù)據(jù)字典自行修改。
總結(jié)
以上所述是小編給大家介紹的基于SpringBoot實(shí)現(xiàn)用戶身份驗(yàn)證工具,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
JDK8配置環(huán)境變量的bat文件的詳細(xì)教程
這篇文章主要介紹了JDK8配置環(huán)境變量的bat文件,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
mybatis創(chuàng)建項(xiàng)目報(bào)Invalid?bound?statement?(not?found)錯誤解決方法
使用MyBatis能夠幫助我們將SQL語句和Java代碼分離,這篇文章主要給大家介紹了關(guān)于mybatis創(chuàng)建項(xiàng)目報(bào)Invalid?bound?statement?(not?found)錯誤的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
SpringBoot定時任務(wù)不執(zhí)行的幾個可能原因及解決方法
這篇文章主要介紹了SpringBoot定時任務(wù)不執(zhí)行的幾個可能原因及解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12
java對象數(shù)組實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java對象數(shù)組實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(5)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
Java使用Callable和Future創(chuàng)建線程操作示例
這篇文章主要介紹了Java使用Callable和Future創(chuàng)建線程操作,結(jié)合實(shí)例形式分析了java使用Callable接口和Future類創(chuàng)建線程的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-09-09
Spring?Boot接口支持高并發(fā)具體實(shí)現(xiàn)代碼
這篇文章主要給大家介紹了關(guān)于Spring?Boot接口支持高并發(fā)具體實(shí)現(xiàn)的相關(guān)資料,在SpringBoot項(xiàng)目中通常我們沒有處理并發(fā)問題,但是使用項(xiàng)目本身還是支持一定的并發(fā)量,需要的朋友可以參考下2023-08-08

