teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

情報追加

2019/10/05 23:05

投稿

syachiku
syachiku

スコア117

title CHANGED
File without changes
body CHANGED
@@ -65,8 +65,34 @@
65
65
  @Override
66
66
  public void postHandle(HttpServletRequest request, HttpServletResponse response,
67
67
  Object handler, ModelAndView modelAndView) throws Exception {
68
+ if (modelAndView != null && !isRedirectView(modelAndView)) {
69
+ if (isUserLogged()) {
68
- //ログインユーザの取得してModelAndViewにセットする処理
70
+ addToModelUserDetails(modelAndView);
71
+ }
72
+ }
69
73
  }
74
+
75
+ public static boolean isRedirectView(ModelAndView mv) {
76
+ String viewName = mv.getViewName();
77
+ if (viewName.startsWith("redirect:/")) {
78
+ return true;
79
+ }
80
+ View view = mv.getView();
81
+ return (view != null && view instanceof SmartView && ((SmartView) view).isRedirectView());
82
+ }
83
+
84
+ public static boolean isUserLogged() {
85
+ try {
86
+ return !SecurityContextHolder.getContext().getAuthentication().getName().equals("anonymousUser");
87
+ } catch (Exception e) {
88
+ return false;
89
+ }
90
+ }
91
+
92
+ private void addToModelUserDetails(ModelAndView model) {
93
+ String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
94
+ model.addObject("loggedUsername", loggedUsername);
95
+ }
70
96
  }
71
97
  ```
72
98
 

1

タイトルに情報追加

2019/10/05 23:05

投稿

syachiku
syachiku

スコア117

title CHANGED
@@ -1,1 +1,1 @@
1
- Spring boot2でSpringSecurityとInterceptorの設定を行うとstaticフォルダ内のcssにアクセスできなくなる
1
+ Spring boot2でSpringSecurityとInterceptorの設定を行うとstaticフォルダ内のcssやjsにアクセスできなくなる
body CHANGED
File without changes