質問編集履歴

2

コメントの編集

2020/07/28 05:45

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,9 @@
4
4
 
5
5
  しかし、現状のコードではIDとパスワードを入力してログインしようとしてもログイン画面に戻される状態となっています。
6
6
 
7
+
8
+
7
- コードの間違いの指摘や書き方のをしていただけると幸いで
9
+ ※フィルタのコードを書き換えました。ログイン処理以外にフィルタを実施するための書き方が分からないでご教示いただけいでしょうか
8
10
 
9
11
  ```ここに言語を入力
10
12
 

1

フィルタのコードを書き直し、ログインコントローラのコードも載せました。フィルタの続きの書き方(ログイン処理以外にフィルタを実施する方法)が分からないのでご教示いただけると幸いです。

2020/07/28 05:45

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -8,85 +8,93 @@
8
8
 
9
9
  ```ここに言語を入力
10
10
 
11
- コー
11
+ ログインントロ
12
12
 
13
- @Component
13
+ @Controller
14
14
 
15
- public class LoginCheckFilter implements Filter {
15
+ public class LoginController {
16
16
 
17
- @Override
17
+
18
18
 
19
- public void doFilter(ServletRequest request, ServletResponse response,
19
+ /**
20
20
 
21
+ * 社員情報
22
+
23
+ */
24
+
25
+ @Autowired
26
+
27
+ EmployeeRepository employeeRepository;
28
+
29
+
30
+
31
+ /**
32
+
33
+ * セッション情報
34
+
35
+ */
36
+
37
+ @Autowired
38
+
39
+ HttpSession session;
40
+
41
+
42
+
43
+ @RequestMapping(path = "/login", method = RequestMethod.GET)
44
+
21
- FilterChain chain) throws IOException, ServletException {
45
+ public String login(@ModelAttribute LoginForm form) {
46
+
47
+ return "index";
48
+
49
+ }
50
+
51
+
52
+
53
+ @RequestMapping(path = "/login", method = RequestMethod.POST)
54
+
55
+ public String doLogin(@Valid @ModelAttribute LoginForm form, BindingResult result) {
56
+
57
+
58
+
59
+ if (result.hasErrors()) {
60
+
61
+ return login(form);
62
+
63
+ } else {
64
+
65
+ //▼DB接続:ログイン承認
66
+
67
+ Employee employee = employeeRepository.findByEmpIdAndEmpPass(form.getEmpId(), form.getEmpPass());
68
+
69
+ //▲戻り値は 「Employeeのオブジェクトの参照」or「null」
70
+
71
+ session.setAttribute("user", employee);
72
+
73
+ return "redirect:/list";
74
+
75
+ }
76
+
77
+ }
78
+
79
+
80
+
81
+ /**
82
+
83
+ * ログアウト処理
84
+
85
+ */
86
+
87
+ @RequestMapping(path = "/logout", method = RequestMethod.GET)
88
+
89
+ public String logout() {
22
90
 
23
91
 
24
92
 
25
- //ServletRequest型リクエスト情報をHttpServletRequest型(サブクラスの型)にキャストする
93
+ // セッション破棄
26
94
 
27
- HttpServletRequest httpRequest = (HttpServletRequest) request;
95
+ session.invalidate();
28
96
 
29
- String requestURL = httpRequest.getRequestURI();
30
-
31
-
32
-
33
- if (requestURL.endsWith("/login")) {
97
+ return "redirect:/login";
34
-
35
- // リクエストURLが「ログイン画面への遷移処理」、
36
-
37
- // 「ログイン処理」宛の場合、ログインチェックを実施せず、
38
-
39
- // リクエスト対象のコントローラの処理に移る
40
-
41
- chain.doFilter(request, response);
42
-
43
- } else {
44
-
45
- // セッション情報を取得
46
-
47
- HttpSession session = httpRequest.getSession();
48
-
49
-
50
-
51
- // セッション情報からユーザのログイン情報(社員ID)を取得
52
-
53
- Integer empId = (Integer) session.getAttribute("empId");
54
-
55
-
56
-
57
- if (empId == null) {
58
-
59
- // ログイン情報が存在しない場合(ログインIDがnullの場合)、
60
-
61
- // ログイン画面にリダイレクトする
62
-
63
-
64
-
65
- // ServletResponse型のリクエスト情報を
66
-
67
- // HttpServletResponse型(サブクラスの型)にキャストする
68
-
69
- HttpServletResponse httpResponse = (HttpServletResponse) response;
70
-
71
-
72
-
73
- // ログイン画面にリダイレクトする
74
-
75
- httpResponse.sendRedirect("/spring_crud/login");
76
-
77
- } else {
78
-
79
- // ログイン情報が存在する場合
80
-
81
- // (ログインIDに何らかの文字列が保存されている場合)、
82
-
83
- // リクエスト対象のコントローラの処理に移る
84
-
85
- chain.doFilter(request, response);
86
-
87
- }
88
-
89
- }
90
98
 
91
99
  }
92
100
 
@@ -98,106 +106,62 @@
98
106
 
99
107
  ```ここに言語を入力
100
108
 
101
- コード
109
+ ログインチェックフィルタ
102
110
 
103
- <!DOCTYPE html>
111
+ @Component
104
112
 
105
- <html xmlns:th="http://www.thymeleaf.org">
113
+ public class LoginCheckFilter implements Filter {
106
114
 
107
- <head>
115
+ @Override
108
116
 
109
- <meta charset="UTF-8" />
117
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
110
118
 
111
- <link rel="stylesheet" type="text/css" th:href="@{/css/layout.css}" />
119
+ throws IOException, ServletException {
112
120
 
113
- <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}" />
121
+ // リクエスト情報を取得
114
122
 
115
- <title>社員管理システム</title>
116
-
117
- </head>
118
-
119
- <body th:object="${loginForm}">
120
-
121
- <header>
122
-
123
- <div class="content">
124
-
125
- <div class="title">社員管理システム</div>
123
+ HttpServletRequest httpRequest = (HttpServletRequest) request;
126
-
127
- </div>
128
-
129
- </header>
130
124
 
131
125
 
132
126
 
133
- <div class="content">
127
+ if (checkRequestURL(httpRequest)) {
134
-
135
- <article>
136
-
137
- <h3>ログイン画面</h3>
138
-
139
- <div class="form">
140
-
141
-
142
-
143
- <p>
144
-
145
- <span th:each="error:${#fields.detailedErrors()}">
146
-
147
- <span th:text="${error.message}"></span><br/>
148
-
149
- </span>
150
-
151
- </p>
152
-
153
-
154
-
155
- <form method="post" th:action="@{/login}">
156
-
157
- <div class="login_label">社員ID</div>
158
-
159
- <div class="login_input">
160
-
161
- <input type="text" th:field="*{empId}" />
162
-
163
- </div>
164
-
165
- <div class="login_label">パスワード</div>
166
-
167
- <div class="login_input">
168
-
169
- <input type="password" th:field="*{empPass}" />
170
-
171
- </div>
172
-
173
- <div class="login_label"></div>
174
-
175
- <div class="login_input">
176
-
177
- <input type="submit" value="ログイン" />
178
-
179
- </div>
180
-
181
- </form>
182
-
183
- </div>
184
-
185
- </article>
186
-
187
- </div>
188
128
 
189
129
 
190
130
 
191
- <footer>
131
+ // セッション情報を取得
192
132
 
193
- <div class="content">Copyright(C) 2017 System Shared co., ltd, ALL Rights Reserved</div>
133
+ HttpSession session = httpRequest.getSession();
194
134
 
195
- </footer>
196
135
 
197
-
198
136
 
199
- </body>
137
+ if (session.getAttribute("user") == null) {
200
138
 
139
+ // 不正アクセスの場合、ログイン画面にリダイレクト
140
+
141
+
142
+
201
- </html>
143
+ // レスポンス情報を取得
144
+
145
+ HttpServletResponse httpResponse = (HttpServletResponse) response;
146
+
147
+
148
+
149
+ // ログイン画面へリダイレクト
150
+
151
+ httpResponse.sendRedirect(httpRequest.getContextPath() + "/login");
152
+
153
+ } else {
154
+
155
+ chain.doFilter(request, response);
156
+
157
+ }
158
+
159
+ } else {
160
+
161
+ chain.doFilter(request, response);
162
+
163
+ }
164
+
165
+ }
202
166
 
203
167
  ```