基于SpringBoot實(shí)現(xiàn)用戶身份驗(yàn)證工具
session失效時(shí)間
在Tomcat上,session的默認(rèn)有效時(shí)間是30分鐘。也可以通過配置文件修改session的有效時(shí)間。
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 = #會(huì)話超時(shí)(秒)
使用過濾器獲取session進(jìn)行身份驗(yàn)證(未全部測(cè)試,慎用)
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ì)心的讀者會(huì)發(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)容請(qǐng)根據(jù)項(xiàng)目需求及數(shù)據(jù)字典自行修改。
總結(jié)
以上所述是小編給大家介紹的基于SpringBoot實(shí)現(xiàn)用戶身份驗(yàn)證工具,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
jvm中指定時(shí)區(qū)信息user.timezone問題及解決方式
同一份程序使用時(shí)間LocalDateTime類型,在國(guó)內(nèi)和國(guó)外部署后,返回的時(shí)間信息前端使用出問題,這篇文章主要介紹了jvm中指定時(shí)區(qū)信息user.timezone問題及解決方法,需要的朋友可以參考下2023-02-02
Java實(shí)現(xiàn)雙色球抽獎(jiǎng)隨機(jī)算法示例
本篇文章主要介紹了Java實(shí)現(xiàn)雙色球抽獎(jiǎng)隨機(jī)算法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Java傳值調(diào)用和傳引用調(diào)用方式(參數(shù)引用為null問題)
這篇文章主要介紹了Java傳值調(diào)用和傳引用調(diào)用方式(參數(shù)引用為null問題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
在Mac OS上安裝Java以及配置環(huán)境變量的基本方法
這篇文章主要介紹了在Mac OS上安裝Java以及配置環(huán)境變量的基本方法,包括查看所安裝Java版本的方法,需要的朋友可以參考下2015-10-10
IDEA中Mybatis的MGB使用逆向工程配置的詳細(xì)教程
這篇文章主要介紹了IDEA中Mybatis的MGB使用逆向工程配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

