0
0

More than 3 years have passed since last update.

Java Tokenintercepterを使用して、Sessionの確認

Last updated at Posted at 2021-03-30

TokenInterceptorとは

Controllerの実行前後に処理を追加する

使用環境

Java
Springboot
SpringToolSweet4

実装方法

・プロジェクト構造
スクリーンショット 2021-03-30 16.52.20.png

以下ファイルを作成

TokenInterceptor.java
package com.example.demo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

// HandlerInterceptorAdapterを継承
@SuppressWarnings("deprecation")
public class TokenInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    HttpSession session;

    // ①コントローラメソッドの実行前に呼ばれる
    @Override
    public boolean preHandle(
                    HttpServletRequest request,
                    HttpServletResponse response,
                    Object handler) throws Exception {

        //セッション取得
        String sessiondata;
        sessiondata = request.getSession().getId();

        //セッション有効時間取得
        int intervalTime = session.getMaxInactiveInterval();

        //セッション保存データ取得
        String user = (String) session.getAttribute("sessiondata");

        System.out.println(sessiondata);
        System.out.println(user);
        System.out.println(intervalTime);

        if(intervalTime==1800) {
            session.setMaxInactiveInterval(180);
        }

        //Interceptorで全指定しているので、最初はnullになる為、nullの場合はスルーする
        if(!(user==null)) {
            if (!(sessiondata.equals(user))) {
                throw new NotValidSessionException("セッションが切れました。再度ログインして下さい。");
            }       
        }

        System.out.println("preHandle");
        return true;
    }


    // ②コントローラメソッドの実行後に呼ばれる
    @Override
    public void postHandle(
                        HttpServletRequest request,
                        HttpServletResponse response,
                        Object handler,
                        ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }

    // ③リクエスト処理の完了後に呼ばれる
    @Override
    public void afterCompletion(
                        HttpServletRequest request,
                        HttpServletResponse response,
                        Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}

以下に記載されているパス配下のファイルに適用される

spring-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:*.properties" />

    <!-- インターセプタの定義 -->
     <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.example.demo.TokenInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

@SpringBootApplicationがついているクラスに上記XMLを読み込ませる

Demo24Application.java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@Configuration
@ImportResource("spring-context.xml")
public class Demo24Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo24Application.class, args);
    }

}

アプリケーション起動後にログにSession情報が出力されていることを確認

スクリーンショット 2021-03-30 18.46.01.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0