質問編集履歴

4

2018/10/23 08:52

投稿

mk222222
mk222222

スコア59

test CHANGED
File without changes
test CHANGED
@@ -94,78 +94,74 @@
94
94
 
95
95
  ```
96
96
 
97
+ 追記
98
+
99
+ 下記ベストアンサーを参考に実装してみたのですが
100
+
101
+ nullというエラーが出ました。
102
+
103
+ デバッグしてauthRequestああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああーめん
104
+
105
+ ```ここに言語を入力
106
+
107
+ public Authentication attemptAuthentication(HttpServletRequest request,
108
+
97
- UsernamePasswordAuthenticationFilterのクラス拡張してみました
109
+ HttpServletResponse response) throws AuthenticationException {
98
110
 
99
111
 
100
112
 
101
- ```ここに言語を入力
113
+ if (!request.getMethod().equals("POST")) {
102
114
 
103
- public class ExUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
115
+ throw new AuthenticationServiceException("Authentication method not supported: "
116
+
117
+ + request.getMethod());
118
+
119
+ }
104
120
 
105
121
 
106
122
 
107
- @Override
123
+ // Obtain UserName, Password, CompanyId
108
124
 
109
- public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) {
125
+ String username = super.obtainUsername(request);
110
126
 
111
- final String car = request.getParameter("car");
127
+ String password = super.obtainPassword(request);
112
128
 
129
+ String carNo = obtainCarNo(request);
130
+
113
- request.getSession().setAttribute("car", car);
131
+ request.setAttribute("carNo", carNo);
132
+
133
+ // username required
134
+
135
+ if (!StringUtils.hasText(username)) {
136
+
137
+ throw new AuthenticationServiceException("UserName is required");
138
+
139
+ }
114
140
 
115
141
 
116
142
 
143
+ // validate password, companyId
144
+
145
+
146
+
147
+ // omitted other process
148
+
149
+
150
+
117
- return super.attemptAuthentication(request, response);
151
+ CarNoUsernamePasswordAuthenticationToken authRequest =
152
+
153
+ new CarNoUsernamePasswordAuthenticationToken(username, password, carNo);
154
+
155
+
156
+
157
+ // Allow subclasses to set the "details" property
158
+
159
+ setDetails(request, authRequest);
160
+
161
+
162
+
163
+ return this.getAuthenticationManager().authenticate(authRequest); // ここでnull
118
164
 
119
165
  }
120
166
 
121
- }
122
-
123
167
  ```
124
-
125
-
126
-
127
- そしてjavaconfigに追記
128
-
129
- ```ここに言語を入力
130
-
131
- @Override
132
-
133
- protected void configure(HttpSecurity http) throws Exception {
134
-
135
-
136
-
137
- // カスタムフィルター
138
-
139
- http.addFilter(new ExUsernamePasswordAuthenticationFilter());
140
-
141
- // ログイン処理
142
-
143
- http.authorizeRequests()
144
-
145
- .antMatchers("/js/**","css/**").permitAll()
146
-
147
- .antMatchers("/**").authenticated()
148
-
149
- .and().formLogin()
150
-
151
- .loginPage("/login")
152
-
153
- .usernameParameter("username")
154
-
155
- .passwordParameter("password")
156
-
157
- .failureUrl("/login").permitAll();
158
-
159
- ```
160
-
161
- そしたら以下のエラー、
162
-
163
- ```ここに言語を入力
164
-
165
- null
166
-
167
- at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94) ~[spring-security-web-5.1.1.RELEASE.jar:5.1.1.RELEASE]
168
-
169
- ```
170
-
171
- 難しいですね。。

3

2018/10/23 08:52

投稿

mk222222
mk222222

スコア59

test CHANGED
File without changes
test CHANGED
@@ -154,8 +154,6 @@
154
154
 
155
155
  .passwordParameter("password")
156
156
 
157
- .defaultSuccessUrl("/searchDelivery",true)
158
-
159
157
  .failureUrl("/login").permitAll();
160
158
 
161
159
  ```

2

2018/10/23 02:10

投稿

mk222222
mk222222

スコア59

test CHANGED
File without changes
test CHANGED
@@ -108,7 +108,7 @@
108
108
 
109
109
  public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) {
110
110
 
111
- final String carNo = request.getParameter("car");
111
+ final String car = request.getParameter("car");
112
112
 
113
113
  request.getSession().setAttribute("car", car);
114
114
 

1

追記

2018/10/23 02:06

投稿

mk222222
mk222222

スコア59

test CHANGED
File without changes
test CHANGED
@@ -19,6 +19,8 @@
19
19
  わかる方、よろしくお願いいたします。
20
20
 
21
21
 
22
+
23
+ securityのjavaConfig
22
24
 
23
25
  ```ここに言語を入力
24
26
 
@@ -49,3 +51,123 @@
49
51
  }
50
52
 
51
53
  ```
54
+
55
+ ログイン画面
56
+
57
+ ```ここに言語を入力
58
+
59
+ <form th:action="@{/login}" method="post">
60
+
61
+ <table>
62
+
63
+ <tr>
64
+
65
+ <td></td>
66
+
67
+ <td><input type="text" name="car"></td>//こいつもsessionに格納したい
68
+
69
+ </tr>
70
+
71
+ <tr>
72
+
73
+ <td>ユーザーコード</td>
74
+
75
+ <td><input type="text" name="username" /></td>
76
+
77
+ </tr>
78
+
79
+ <tr>
80
+
81
+ <td>パスワード</td>
82
+
83
+ <td><input type="password" name="password" /></td>
84
+
85
+ </tr>
86
+
87
+ </table>
88
+
89
+ <br /><br />
90
+
91
+ <button type="submit">ログイン</button>
92
+
93
+ </form>
94
+
95
+ ```
96
+
97
+ UsernamePasswordAuthenticationFilterのクラス拡張してみました
98
+
99
+
100
+
101
+ ```ここに言語を入力
102
+
103
+ public class ExUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
104
+
105
+
106
+
107
+ @Override
108
+
109
+ public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) {
110
+
111
+ final String carNo = request.getParameter("car");
112
+
113
+ request.getSession().setAttribute("car", car);
114
+
115
+
116
+
117
+ return super.attemptAuthentication(request, response);
118
+
119
+ }
120
+
121
+ }
122
+
123
+ ```
124
+
125
+
126
+
127
+ そしてjavaconfigに追記
128
+
129
+ ```ここに言語を入力
130
+
131
+ @Override
132
+
133
+ protected void configure(HttpSecurity http) throws Exception {
134
+
135
+
136
+
137
+ // カスタムフィルター
138
+
139
+ http.addFilter(new ExUsernamePasswordAuthenticationFilter());
140
+
141
+ // ログイン処理
142
+
143
+ http.authorizeRequests()
144
+
145
+ .antMatchers("/js/**","css/**").permitAll()
146
+
147
+ .antMatchers("/**").authenticated()
148
+
149
+ .and().formLogin()
150
+
151
+ .loginPage("/login")
152
+
153
+ .usernameParameter("username")
154
+
155
+ .passwordParameter("password")
156
+
157
+ .defaultSuccessUrl("/searchDelivery",true)
158
+
159
+ .failureUrl("/login").permitAll();
160
+
161
+ ```
162
+
163
+ そしたら以下のエラー、
164
+
165
+ ```ここに言語を入力
166
+
167
+ null
168
+
169
+ at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94) ~[spring-security-web-5.1.1.RELEASE.jar:5.1.1.RELEASE]
170
+
171
+ ```
172
+
173
+ 難しいですね。。