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

質問編集履歴

2

コメントの編集

2020/07/28 05:45

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,8 @@
1
1
  Springでのログインチェックの方法が分からず困っています。
2
2
  未ログイン状態でログイン画面以外の画面を表示しようとした際に、ログイン画面に遷移させる処理を行いたいです。
3
3
  しかし、現状のコードではIDとパスワードを入力してログインしようとしてもログイン画面に戻される状態となっています。
4
+
4
- コードの間違いの指摘や書き方のをしていただけると幸いで
5
+ ※フィルタのコードを書き換えました。ログイン処理以外にフィルタを実施するための書き方が分からないでご教示いただけいでしょうか
5
6
  ```ここに言語を入力
6
7
  ログインコントローラ
7
8
  @Controller

1

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

2020/07/28 05:45

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -3,100 +3,82 @@
3
3
  しかし、現状のコードではIDとパスワードを入力してログインしようとしてもログイン画面に戻される状態となっています。
4
4
  コードの間違いの指摘や書き方の提示をしていただけると幸いです。
5
5
  ```ここに言語を入力
6
+ ログインコントローラ
7
+ @Controller
8
+ public class LoginController {
9
+
10
+ /**
11
+ * 社員情報
12
+ */
13
+ @Autowired
14
+ EmployeeRepository employeeRepository;
15
+
16
+ /**
17
+ * セッション情報
18
+ */
19
+ @Autowired
20
+ HttpSession session;
21
+
22
+ @RequestMapping(path = "/login", method = RequestMethod.GET)
23
+ public String login(@ModelAttribute LoginForm form) {
24
+ return "index";
25
+ }
26
+
27
+ @RequestMapping(path = "/login", method = RequestMethod.POST)
28
+ public String doLogin(@Valid @ModelAttribute LoginForm form, BindingResult result) {
29
+
30
+ if (result.hasErrors()) {
31
+ return login(form);
32
+ } else {
33
+ //▼DB接続:ログイン承認
34
+ Employee employee = employeeRepository.findByEmpIdAndEmpPass(form.getEmpId(), form.getEmpPass());
35
+ //▲戻り値は 「Employeeのオブジェクトの参照」or「null」
36
+ session.setAttribute("user", employee);
37
+ return "redirect:/list";
38
+ }
39
+ }
40
+
41
+ /**
42
+ * ログアウト処理
43
+ */
44
+ @RequestMapping(path = "/logout", method = RequestMethod.GET)
45
+ public String logout() {
46
+
47
+ // セッションの破棄
48
+ session.invalidate();
49
+ return "redirect:/login";
50
+ }
51
+
52
+ }
6
- コード
53
+ ```
54
+ ```ここに言語を入力
55
+ ログインチェックフィルタ
7
56
  @Component
8
57
  public class LoginCheckFilter implements Filter {
9
58
  @Override
10
- public void doFilter(ServletRequest request, ServletResponse response,
59
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
11
- FilterChain chain) throws IOException, ServletException {
60
+ throws IOException, ServletException {
12
-
13
- //ServletRequest型のリクエスト情報をHttpServletRequest型(サブクラスの型)にキャストする
61
+ // リクエスト情報を取得
14
62
  HttpServletRequest httpRequest = (HttpServletRequest) request;
63
+
15
- String requestURL = httpRequest.getRequestURI();
64
+ if (checkRequestURL(httpRequest)) {
16
-
17
- if (requestURL.endsWith("/login")) {
65
+
18
- // リクエストURLが「ログイン画面への遷移処理」、
19
- // 「ログイン処理」宛の場合、ログインチェックを実施せず、
20
- // リクエスト対象のコントローラの処理に移る
21
- chain.doFilter(request, response);
22
- } else {
23
66
  // セッション情報を取得
24
67
  HttpSession session = httpRequest.getSession();
25
-
26
- // セッション情報からユーザのログイン情報(社員ID)を取得
68
+
27
- Integer empId = (Integer) session.getAttribute("empId");
69
+ if (session.getAttribute("user") == null) {
28
-
29
- if (empId == null) {
30
- // ログイン情報が存在しない場合(ログインIDがnullの場合)、
31
- // ログイン画面にリダイレクトする
70
+ // 不正アクセスの場合、ログイン画面にリダイレクト
32
-
71
+
33
- // ServletResponse型のリクエ情報を
72
+ // ポンス情報を取得
34
- // HttpServletResponse型(サブクラスの型)にキャストする
35
73
  HttpServletResponse httpResponse = (HttpServletResponse) response;
36
-
74
+
37
- // ログイン画面リダイレクトする
75
+ // ログイン画面リダイレクト
38
- httpResponse.sendRedirect("/spring_crud/login");
76
+ httpResponse.sendRedirect(httpRequest.getContextPath() + "/login");
39
77
  } else {
40
- // ログイン情報が存在する場合
41
- // (ログインIDに何らかの文字列が保存されている場合)、
42
- // リクエスト対象のコントローラの処理に移る
43
78
  chain.doFilter(request, response);
44
79
  }
80
+ } else {
81
+ chain.doFilter(request, response);
45
82
  }
46
83
  }
47
-
48
- }
49
- ```
50
- ```ここに言語を入力
51
- コード
52
- <!DOCTYPE html>
53
- <html xmlns:th="http://www.thymeleaf.org">
54
- <head>
55
- <meta charset="UTF-8" />
56
- <link rel="stylesheet" type="text/css" th:href="@{/css/layout.css}" />
57
- <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}" />
58
- <title>社員管理システム</title>
59
- </head>
60
- <body th:object="${loginForm}">
61
- <header>
62
- <div class="content">
63
- <div class="title">社員管理システム</div>
64
- </div>
65
- </header>
66
-
67
- <div class="content">
68
- <article>
69
- <h3>ログイン画面</h3>
70
- <div class="form">
71
-
72
- <p>
73
- <span th:each="error:${#fields.detailedErrors()}">
74
- <span th:text="${error.message}"></span><br/>
75
- </span>
76
- </p>
77
-
78
- <form method="post" th:action="@{/login}">
79
- <div class="login_label">社員ID</div>
80
- <div class="login_input">
81
- <input type="text" th:field="*{empId}" />
82
- </div>
83
- <div class="login_label">パスワード</div>
84
- <div class="login_input">
85
- <input type="password" th:field="*{empPass}" />
86
- </div>
87
- <div class="login_label"></div>
88
- <div class="login_input">
89
- <input type="submit" value="ログイン" />
90
- </div>
91
- </form>
92
- </div>
93
- </article>
94
- </div>
95
-
96
- <footer>
97
- <div class="content">Copyright(C) 2017 System Shared co., ltd, ALL Rights Reserved</div>
98
- </footer>
99
-
100
- </body>
101
- </html>
102
84
  ```