基于SpringBoot實現(xiàn)用戶身份驗證工具
session失效時間
在Tomcat上,session的默認有效時間是30分鐘。也可以通過配置文件修改session的有效時間。
1)修改web.xml
<!-- 設置session失效,單位分 --> <session-config> <session-timeout>1</session-timeout> </session-config>
2).yml文件
server.session.cookie.http-only= #是否開啟HttpOnly server.session.timeout = #會話超時(秒)
使用過濾器獲取session進行身份驗證(未全部測試,慎用)
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());
//進行認證
//認證失敗
if(userType == null){
//...
}
//用戶不是管理員
if(userType != UserTypeEnum.ADMIN){
//...
}
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
細心的讀者會發(fā)現(xiàn)我用了AuthenticationUtil,這是為了將讀寫用戶身份認證信息的功能分離而設計的工具類 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類和配置類的內容請根據(jù)項目需求及數(shù)據(jù)字典自行修改。
總結
以上所述是小編給大家介紹的基于SpringBoot實現(xiàn)用戶身份驗證工具,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
mybatis創(chuàng)建項目報Invalid?bound?statement?(not?found)錯誤解決方法
使用MyBatis能夠幫助我們將SQL語句和Java代碼分離,這篇文章主要給大家介紹了關于mybatis創(chuàng)建項目報Invalid?bound?statement?(not?found)錯誤的解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05
SpringBoot定時任務不執(zhí)行的幾個可能原因及解決方法
這篇文章主要介紹了SpringBoot定時任務不執(zhí)行的幾個可能原因及解決方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12
java對象數(shù)組實現(xiàn)學生信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了java對象數(shù)組實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
Java使用Callable和Future創(chuàng)建線程操作示例
這篇文章主要介紹了Java使用Callable和Future創(chuàng)建線程操作,結合實例形式分析了java使用Callable接口和Future類創(chuàng)建線程的相關操作技巧與注意事項,需要的朋友可以參考下2019-09-09
Spring?Boot接口支持高并發(fā)具體實現(xiàn)代碼
這篇文章主要給大家介紹了關于Spring?Boot接口支持高并發(fā)具體實現(xiàn)的相關資料,在SpringBoot項目中通常我們沒有處理并發(fā)問題,但是使用項目本身還是支持一定的并發(fā)量,需要的朋友可以參考下2023-08-08

