セッションの有効期限
Tomcatでは、セッションのデフォルトの有効な時間は30分です。構成ファイルを介してセッションの有効な時間を変更することもできます。
1)web.xmlを変更します
<! - セッション無効、ユニットセグメント - > <session-config> <session-timeout> 1 </session-timeout> </session-config>
2).ymlファイル
server.session.cookie.httpのみ=#httponly server.session.timeout = #session timeout(seconds)を有効にするかどうか
フィルターを使用して、認証用のセッションを取得します(すべてのテストはテストされていません。慎重に使用してください)
1)新しいフィルターを作成します
Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.boot.web.servlet.servletcomponentscan; org.springframework.context.applicationcontextをインポートします。 org.springframework.stereotype.componentをインポートします。 Import org.springframework.web.context.support.webapplicationContextutils; javax.servlet.annotation.webfilterをインポートします。 javax.servlet.http.httpservletrequestをインポートします。 java.io.ioexceptionをインポートします。 @component @servletcomponentscan // @webfilter work @webfilter(urlpatterns = "/*")public class myfilterを実装します{@autowired privatekeyconfigproperties sessionkeyconfigproperties; @Override public void init(filterconfig filterconfig)servletexception {} @override public void dofilter(servletRequest servletRequest、servletResponse servletResponse、filterchain filterchain)をスローします。 system.out.println(sessionkeyconfigproperties.getusypekekey()); // session authenticationutiluticationutil = new Authenticationutil(sessionkeyconfigproperties)を使用してID情報を取得します。 usertypeenum usertype = authenticationutil.getuserauthentication(httpservletrequest.getSession()); //認定//認証が失敗した場合(usertype == null){// ...} //ユーザーは管理者ではありません。 } @Override public void Destroy(){}}注意深い読者は、ユーザーIDの認証情報を読み書きする機能を分離するために設計されたツールクラスであるAuthenticationUtilを使用していることがわかります。 2)AuthenticationUtilクラス
Import org.apache.shiro.web.session.httpservletessession; javax.servlet.http.httpservletrequestをインポートします。 javax.servlet.http.httpsessionをインポートします。 public class authenticationutil {private sessionkeyconfigproperties configproperties; public AuthenticationUtil(sessionkeyconfigproperties configproperties){this.configproperties = configProperties; } /** *セッションからユーザーのIDタイプを取得 * @Param Session * @return IDタイプ * /public usertypeenum getUserAuthentication(httpsession Session){//セッションオブジェクトでユーザー情報レコードを取得ogutt usertype = session.getattribute(configproperties.getusertypekekey(); //セッションでユーザータイプを記録します(usertype!= null && usertype intance of usertypeenum){return(usertypeenum)usertype; } nullを返します。 } / ** *ユーザーのIDをセッションに書き込みます * @param Session * @param usertype * / public void setuserauthentication(httpssession session、usertypeenum usertype){session.setattribute(configproperties.getusypekey()、usertype); }} 3)構成ファイルsessiionkeyconfig.properties
user_type_key = userTypeKey
4)読み取りファイルsessionkeyconfigproperties.classを構成します
Import org.springframework.beans.factory.annotation.value; org.springframework.context.annotation.configurationをインポートします。 org.springframework.context.annotation.propertysourceをインポートします。 org.springframework.steretype.componentをインポートします。 @configuration @propertysource( "classpath:config/sessionkeyconfig.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)列挙クラス
public enum usertypeenum {admin、user}注:この記事では、一部のパッケージ情報とインポート情報を削除します。プロジェクトの要件とデータ辞書に従って、Enumクラスと構成クラスのコンテンツを自分で変更してください。
要約します
上記は、エディターが紹介したスプリングブートベースのユーザー認証ツールです。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!