Spring Boot/認証/スクラッチ/HelloWorld/080_未ログイン時の挙動

Spring Boot/認証/スクラッチ/HelloWorld/080_未ログイン時の挙動

Prev

未ログイン時の挙動

現状では未ログイン状態である場合レスポンス真っ白になってしまうのでしかるべき画面に飛ばしたい。

ログイン画面に飛ばすことにする。

こんな感じで response を操作してやればいい

public class MyAuthInterceptor implements HandlerInterceptor{
    @Autowired
    private AuthUser authUser;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod h = (HandlerMethod) handler;
        Method m = h.getMethod();
        NoAuth a = AnnotationUtils.findAnnotation(m, NoAuth.class);
        if(a != null) {
            return true;
        }
        if(!authUser.isLoginFlg()) {
            //no login
            response.sendRedirect("/login");
            return false;
        }
        return true;
    }
}

Next

java/spring/spring_boot/auth/scratch/helloworld/080_proc_nologin.txt · 最終更新: 2019-06-28 11:06 by ore