세션 만료 시간
Tomcat에서는 세션의 기본 유효한 시간은 30 분입니다. 구성 파일을 통해 세션의 유효한 시간을 수정할 수도 있습니다.
1) Web.xml을 수정하십시오
<!-세션 세션 설정 유효하지 않은 세그먼트-> <세션 -Config> <Session-TimeOut> 1 </session-timeout> </session-config>
2) .yml 파일
server.session.cookie.http-soly = # httponly server.session.timeout = #session timeout (초)
필터를 사용하여 인증을 위해 세션을 얻습니다 (모든 테스트는 테스트되지 않으며주의해서 사용하십시오).
1) 새 필터를 만듭니다
org.springframework.beans.factory.annotation.autowired; org.springframework.boot.web.servlet.servletcomponentscan; import org.springframework.context.applicationcontext; org.springframework.stereotyp.component import; import org.springframework.web.context.support.webapplicationcontextutils; javax.servlet.annotation.webfilter import; import javax.servlet.http.httpservletrequest; import java.io.ioexception; @component @servletcomponentscan // @webfilter work @webfilter (urlpatterns = "/*") public class myfilter 구현 필터 {@autowized private sessionkeyConfigProperties sessionKeyConfigProperTies; @override public void init (FilterConfig FilterConfig)는 servleTexception {} @Override public void dofilter (servletRequest servletrequest, servletResponse servletResponse, filterchain filterchain)를 rowsection, servletexception {httpercepcept {httpervletrecept} ServletRequest; System.out.println (SessionKeyConfigProperties.GetUserTypekey ()); // 세션 AuthenticationUtil AuthenticationUtil을 통해 신원 정보를 얻습니다. New AuthenticationUtil (SessionKeyConfigProperties); usertypeenum usertype = authenticationUtil.getUserAuthentication (httpservletRequest.getSession ()); // 인증 // 인증 실패 if (userType == null) {// ...} // 사용자는 관리자가 아닙니다. } @override public void destroy () {}} 신중한 독자는 사용자 ID 인증 정보를 읽고 쓰는 기능을 분리하도록 설계된 도구 클래스 인 AuthenticationUtil을 사용했음을 알게 될 것입니다. 2) AuthenticationUtil 클래스
import org.apache.shiro.web.session.httpservletsession; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpsession; 공개 클래스 AuthenticationUtil {private sessionkeyConfigProperties configProperties; Public AuthenticationUtil (SessionKeyConfigProperties configProperties) {this.configProperties = configproperties; } /** * 세션에서 사용자의 신원 유형을 가져옵니다 * @param 세션 * @return Identity Type * /public usertypeenum getUseRuthentication (httpsession session) {// 세션 객체 userType = session.getAttribute에서 사용자 정보 레코드를 얻습니다. // if (userType! = null && userType userTypeEnum의 userType 인스턴스) {return (userTypeenum) userType; } return null; } / ** * 사용자의 신원을 세션에 작성 * @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 구성
org.springframework.beans.factory.annotation.value import; org.springframework.context.annotation.configuration; org.springframework.context.annotation.propertysource; org.springframework.steretype.component import; @Configuration @PropertySource ( "classPath : config/sessiionkeyConfig.properties") @component public class sessionKeyConfigProperTies {@Value ( "$ {user_type_key}") 개인 문자열 userTypekey; 공개 문자열 getUserTypekey () {return userTypekey; } public void setUserTypekey (String userTypekey) {this.userTypekey = userTypekey; }} 5) 열거 클래스
public enum usertypeenum {admin, user}참고 :이 기사는 일부 패키지 정보 및 일부 가져 오기 정보를 삭제합니다. 프로젝트 요구 사항 및 데이터 사전에 따라 열거 클래스 및 구성 클래스의 내용을 직접 수정하십시오.
요약
위는 편집기가 소개 한 SpringBoot 기반 사용자 인증 도구입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!