回答編集履歴

1

動作確認した上で、質問者が記載すべき設定の足りない個所をすべて記載しています。

2017/06/03 06:16

投稿

A-pZ
A-pZ

スコア12011

test CHANGED
@@ -1,4 +1,8 @@
1
+ いくつか必要な設定が書かれてないので、すでに適用済みかも知れませんが、その場合ご容赦ください。
2
+
3
+
4
+
1
- SQLが通らなそう
5
+ 1 ユーザー認証のSQLが通りませんの、以下に修正してください
2
6
 
3
7
 
4
8
 
@@ -6,14 +10,264 @@
6
10
 
7
11
  private static final String USER_QUERY
8
12
 
9
- ="select name, password, 1"
10
-
11
- +"from Newaccount"
12
-
13
- + "where name = ?";
14
-
15
- ```
16
-
17
-
18
-
19
- なので、1 と from の間と、Newaccountとwheheの間に半角スペースを入れましょう。
13
+ = "select name, password, 1 from Newaccount where name = ?";
14
+
15
+ ```
16
+
17
+
18
+
19
+ 2 アクセス制御ロールを取得するSQLが定義されていませんので、SpringSecurityは以下のSQLで自動的に発行されます。
20
+
21
+
22
+
23
+ ```SQL
24
+
25
+ select username, authority from authorities where username = ?
26
+
27
+ ```
28
+
29
+ これを参照するテーブル定義ならびにデータが登録されていないと、認証機能がエラーになります。
30
+
31
+
32
+
33
+ 3 SpringSecirityConfigの認証部分の設定で、パスワードの項目がBCrypt設定されています。パスワードをハッシュ化した値を使っていないのであれば解除すべきでしょう。
34
+
35
+
36
+
37
+ ```
38
+
39
+ @EnableWebSecurity
40
+
41
+ public class SecurityConfig extends WebSecurityConfigurerAdapter{
42
+
43
+
44
+
45
+ @Override
46
+
47
+ public void configure(AuthenticationManagerBuilder auth) throws Exception {
48
+
49
+ auth.jdbcAuthentication()
50
+
51
+ .dataSource(dataSource)
52
+
53
+ .usersByUsernameQuery(USER_QUERY)
54
+
55
+ .passwordEncoder(new BCryptPasswordEncoder()); // BCryptパスワードハッシュ
56
+
57
+ }
58
+
59
+ }
60
+
61
+ ```
62
+
63
+
64
+
65
+ 4 ログイン成功・失敗時のURL
66
+
67
+
68
+
69
+ ログイン成功・失敗URLでHTMLを指定していますが、HTMLを指定すると静的HTMLになりますので、springbootをお使いの場合は、src/main/resources/static に配置されているHTMLが呼び出されます。
70
+
71
+
72
+
73
+ ログイン成功・失敗時の動作が正しく動いているかどうかを確認するために、次のように修正し、src/main/resources/static 以下に それぞれのHTMLを置くと良いでしょう。
74
+
75
+
76
+
77
+ ```java
78
+
79
+ .defaultSuccessUrl("/success.html")
80
+
81
+ .failureUrl("/failure.html")
82
+
83
+
84
+
85
+ ```
86
+
87
+
88
+
89
+ あと余談ですが、Controllerにて、ModelAndViewを引数に指定していますが、メソッド内で特に利用していないので、不要です。
90
+
91
+
92
+
93
+ ```java
94
+
95
+ public ModelAndView NewAccountPost(@ModelAttribute("formModel") Newaccount newaccount,ModelAndView mav){
96
+
97
+ ```
98
+
99
+
100
+
101
+ 他にもライブラリ依存関係を変更されているようですが、その設定が書かれていないため、ソースが参照できるかどうかの判断はしかねます。
102
+
103
+
104
+
105
+ 以下は動作確認のために適用したmaven設定です。参考までに。
106
+
107
+
108
+
109
+ ```xml
110
+
111
+ <?xml version="1.0" encoding="UTF-8"?>
112
+
113
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
114
+
115
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
116
+
117
+ <modelVersion>4.0.0</modelVersion>
118
+
119
+
120
+
121
+ <groupId>com.example</groupId>
122
+
123
+ <artifactId>spring-security-sample</artifactId>
124
+
125
+ <version>0.0.1-SNAPSHOT</version>
126
+
127
+ <packaging>jar</packaging>
128
+
129
+
130
+
131
+ <name>spring-security-sample</name>
132
+
133
+ <description>Demo project for Spring Boot</description>
134
+
135
+
136
+
137
+ <parent>
138
+
139
+ <groupId>org.springframework.boot</groupId>
140
+
141
+ <artifactId>spring-boot-starter-parent</artifactId>
142
+
143
+ <version>1.5.3.RELEASE</version>
144
+
145
+ <relativePath/> <!-- lookup parent from repository -->
146
+
147
+ </parent>
148
+
149
+
150
+
151
+ <properties>
152
+
153
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
154
+
155
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
156
+
157
+ <java.version>1.8</java.version>
158
+
159
+ </properties>
160
+
161
+
162
+
163
+ <dependencies>
164
+
165
+ <dependency>
166
+
167
+ <groupId>org.springframework.boot</groupId>
168
+
169
+ <artifactId>spring-boot-starter-thymeleaf</artifactId>
170
+
171
+ </dependency>
172
+
173
+
174
+
175
+ <dependency>
176
+
177
+ <groupId>org.projectlombok</groupId>
178
+
179
+ <artifactId>lombok</artifactId>
180
+
181
+ <optional>true</optional>
182
+
183
+ </dependency>
184
+
185
+ <dependency>
186
+
187
+ <groupId>org.springframework.boot</groupId>
188
+
189
+ <artifactId>spring-boot-starter-test</artifactId>
190
+
191
+ <scope>test</scope>
192
+
193
+ </dependency>
194
+
195
+ <dependency>
196
+
197
+ <groupId>org.springframework.boot</groupId>
198
+
199
+ <artifactId>spring-boot-starter-security</artifactId>
200
+
201
+ </dependency>
202
+
203
+ <dependency>
204
+
205
+ <groupId>org.springframework.boot</groupId>
206
+
207
+ <artifactId>spring-boot-starter-web</artifactId>
208
+
209
+ </dependency>
210
+
211
+ <dependency>
212
+
213
+ <groupId>org.springframework.boot</groupId>
214
+
215
+ <artifactId>spring-boot-starter-jdbc</artifactId>
216
+
217
+ </dependency>
218
+
219
+ <dependency>
220
+
221
+ <groupId>org.apache.commons</groupId>
222
+
223
+ <artifactId>commons-dbcp2</artifactId>
224
+
225
+ </dependency>
226
+
227
+ <dependency>
228
+
229
+ <groupId>org.mariadb.jdbc</groupId>
230
+
231
+ <artifactId>mariadb-java-client</artifactId>
232
+
233
+ </dependency>
234
+
235
+ <dependency>
236
+
237
+ <groupId>org.springframework.boot</groupId>
238
+
239
+ <artifactId>spring-boot-devtools</artifactId>
240
+
241
+ </dependency>
242
+
243
+ </dependencies>
244
+
245
+
246
+
247
+ <build>
248
+
249
+ <plugins>
250
+
251
+ <plugin>
252
+
253
+ <groupId>org.springframework.boot</groupId>
254
+
255
+ <artifactId>spring-boot-maven-plugin</artifactId>
256
+
257
+ </plugin>
258
+
259
+ </plugins>
260
+
261
+ </build>
262
+
263
+ </project>
264
+
265
+ ```
266
+
267
+
268
+
269
+ 参考資料:
270
+
271
+ [http://docs.spring.io/spring-security/site/docs/4.2.2.RELEASE/reference/htmlsingle/#jc-authentication-jdbc](http://docs.spring.io/spring-security/site/docs/4.2.2.RELEASE/reference/htmlsingle/#jc-authentication-jdbc)
272
+
273
+