เวลาหมดอายุเซสชัน
บน Tomcat เวลาเริ่มต้นที่ถูกต้องสำหรับเซสชันคือ 30 นาที นอกจากนี้คุณยังสามารถแก้ไขเวลาที่ถูกต้องของเซสชันผ่านไฟล์การกำหนดค่า
1) แก้ไข web.xml
<!-ตั้งค่าเซสชันไม่ถูกต้องเซ็กเมนต์หน่วย-> <เซสชั่น-คอนฟิก> <Session-TimeOut> 1 </Session-TimeOut> </ession-config>
2) ไฟล์. yml
Server.session.cookie.http-only = # ไม่ว่าจะเปิดใช้งาน httponly server.session.timeout = #session timeout (วินาที)
ใช้ตัวกรองเพื่อรับเซสชันสำหรับการตรวจสอบ (การทดสอบทั้งหมดไม่ได้ทดสอบใช้ด้วยความระมัดระวัง)
1) สร้างตัวกรองใหม่
นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.boot.web.servlet.servletComponentscan; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.stereotype.component; นำเข้า org.springframework.web.context.support.webapplicationContextUtils; นำเข้า javax.servlet.annotation.webfilter; นำเข้า Javax.servlet.http.httpservletRequest; นำเข้า java.io.ioException; @Component @servletComponentScan // @work @webfilter work @webfilter (urlpatterns = "/*") คลาสสาธารณะ MyFilter ใช้ตัวกรอง {@autowired เซสชันส่วนตัวเซสชัน @Override โมฆะสาธารณะ init (FilterConfig FilterConfig) พ่น ServleTexception {} @Override โมฆะสาธารณะ dofilter (servletrequest servletrequest, servletResponse servletresponse, filterchain filterchain) Servletrequest; System.out.println (SessionKeyConfigProperties.getUserTypeKey ()); // รับข้อมูลประจำตัวผ่านเซสชัน AuthenticationUtil AuthenticationUtil = ใหม่ AuthenticationUtil (SessionKeyConfigProperties); UserTypeenum userType = AuthenticationUtil.GetUserauthentication (httpservletRequest.getSession ()); // การรับรอง // การรับรองความถูกต้องล้มเหลวถ้า (userType == null) {// ... } // ผู้ใช้ไม่ใช่ผู้ดูแลระบบถ้า (userType! = userTypeenum.admin) {// ... } filterChain.dofilter } @Override โมฆะสาธารณะทำลาย () {}} ผู้อ่านอย่างระมัดระวังจะพบว่าฉันใช้ AuthenticationUtil ซึ่งเป็นคลาสเครื่องมือที่ออกแบบมาเพื่อแยกฟังก์ชั่นการอ่านและการเขียนข้อมูลการรับรองความถูกต้องของผู้ใช้ 2) คลาส AuthenticationUtil
นำเข้า org.apache.shiro.web.session.httpservletsession; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpsession; Public Class AuthenticationUtil {Private SessionKeyConfigProperties configProperties; Public AuthenticationUtil (SessionKeyConfigProperties configProperties) {this.configProperties = configProperties; } /** * รับประเภทตัวตนของผู้ใช้จากเซสชัน * @param เซสชัน * @return ประเภทตัวตน * /ผู้ใช้สาธารณะ GetUserauthentication (เซสชัน httpsession) {// รับข้อมูลผู้ใช้บันทึกในเซสชันวัตถุ userType = เซสชัน // รับประเภทผู้ใช้ที่บันทึกไว้ในเซสชันถ้า (userType! = null && อินสแตนซ์ UserType ของ userTypeenum) {return (userTypeenum) userType; } return null; } / ** * เขียนข้อมูลประจำตัวของผู้ใช้ลงในเซสชัน * @param เซสชัน * @param usertype * / โมฆะสาธารณะ setuserauthentication (httpsession session, usertypeenum userType) {session.setAttribute - 3) การกำหนดค่าไฟล์ SessiionkeyConfig.properties
user_type_key = userTypeKey
4) กำหนดค่าไฟล์อ่าน sessionkeyconfigproperties.class
นำเข้า org.springframework.beans.factory.annotation.value; นำเข้า org.springframework.context.annotation.configuration; นำเข้า org.springframework.context.annotation.propertysource; นำเข้า org.springframework.steretype.component; @Configuration @propertysource ("classpath: config/sessiionkeyconfig.properties") @component คลาสสาธารณะคลาสเซสชันเซสชัน KeyConfigProperties {@value ("$ {user_type_key}") สตริงส่วนตัว usertypekey; สตริงสาธารณะ getUserTypeKey () {return userTypeKey; } โมฆะสาธารณะ setUserTypeKey (String userTypeKey) {this.userTypeKey = userTypeKey; - 5) คลาส Enum
public enum usertypeenum {admin, user}หมายเหตุ: บทความนี้ลบข้อมูลแพ็คเกจและข้อมูลนำเข้าบางส่วน โปรดแก้ไขเนื้อหาของคลาส Enum และคลาสการกำหนดค่าด้วยตัวเองตามข้อกำหนดของโครงการและพจนานุกรมข้อมูล
สรุป
ด้านบนเป็นเครื่องมือตรวจสอบความถูกต้องของผู้ใช้ที่ใช้ Springboot ซึ่งแนะนำโดยตัวแก้ไข ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!