質問編集履歴
2
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -168,7 +168,7 @@
|
|
168
168
|
|
169
169
|
|
170
170
|
|
171
|
-
Details
|
171
|
+
LoginUserDetails.java
|
172
172
|
|
173
173
|
```java
|
174
174
|
|
@@ -218,6 +218,168 @@
|
|
218
218
|
|
219
219
|
|
220
220
|
|
221
|
+
|
222
|
+
|
223
|
+
SecurityConfig.java
|
224
|
+
|
225
|
+
```java
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
package com.example.konkatsu;
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
236
|
+
|
237
|
+
import org.springframework.context.annotation.Bean;
|
238
|
+
|
239
|
+
import org.springframework.context.annotation.Configuration;
|
240
|
+
|
241
|
+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
242
|
+
|
243
|
+
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
|
244
|
+
|
245
|
+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
246
|
+
|
247
|
+
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
248
|
+
|
249
|
+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
250
|
+
|
251
|
+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
252
|
+
|
253
|
+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
254
|
+
|
255
|
+
import org.springframework.security.crypto.password.PasswordEncoder;
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
import com.example.konkatsu.service.LoginUserDetailsService;
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
@EnableWebSecurity //Spring Securityの基本的な設定(認証フィルタの設定など)が行われる
|
264
|
+
|
265
|
+
public class SecurityConfig extends WebSecurityConfigurerAdapter { //WebSecurityConfigurerAdapter を継承することでデフォルト設定に対して追加したい箇所だけオーバーライドして設定できる
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
@Override
|
270
|
+
|
271
|
+
public void configure(WebSecurity web) throws Exception { //configure(WebSecurity)メソッドをオーバーライドすることで特定のリクエストに対してセキュリティー設定を無視する設定など、全体に関わる設定ができる
|
272
|
+
|
273
|
+
web.ignoring().antMatchers("/webjars/**", "/css/**"); //「/webjars」「/css」といった静的リソースに対するアクセスにはセキュリティの設定は無視するようにする
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
@Override
|
280
|
+
|
281
|
+
protected void configure(HttpSecurity http) throws Exception { //configure(HttpSecurity)メソッドをオーバーライドすることで、認可の設定やログイン、ログアウトに関する設定ができる
|
282
|
+
|
283
|
+
http
|
284
|
+
|
285
|
+
.authorizeRequests() //認証が必要となるURLを設定
|
286
|
+
|
287
|
+
.antMatchers("/login").permitAll() //「/login」URLは認識不要。任意のユーザーがアクセスできるようにする
|
288
|
+
|
289
|
+
.antMatchers("/RegisterUser").permitAll()
|
290
|
+
|
291
|
+
.antMatchers("/createUser").permitAll()
|
292
|
+
|
293
|
+
.antMatchers("/createProfile").permitAll()
|
294
|
+
|
295
|
+
// .antMatchers("/konkatsu").permitAll()
|
296
|
+
|
297
|
+
.antMatchers("/RegisterUserForm").permitAll() //ユーザー登録は認識不要
|
298
|
+
|
299
|
+
.anyRequest().authenticated() //それ以外のパスには、認証なしでアクセスできないようにする
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
.and()
|
308
|
+
|
309
|
+
.formLogin().loginProcessingUrl("/login") //ログインに関する設定
|
310
|
+
|
311
|
+
.loginPage("/login") //ログインフォーム表示のパス(URL)
|
312
|
+
|
313
|
+
.failureUrl("/login?error") //認証失敗時の遷移先(?errorとつけておくとthymeleafの方でエラーのメッセージを出すときに便利)
|
314
|
+
|
315
|
+
.defaultSuccessUrl("/konkatsu", true) /*認証成功時の遷移先(第2引数のboolean
|
316
|
+
|
317
|
+
true : ログイン画面した後必ずtopにとばされる
|
318
|
+
|
319
|
+
false : (認証されてなくて一度ログイン画面に飛ばされても)ログインしたら指定したURLに飛んでくれる) */
|
320
|
+
|
321
|
+
.usernameParameter("mail").passwordParameter("pass") //メールアドレス、パスワードのパラメータ名を設定(ログイン画面のhtmlのinputのname属性を見に行っている)
|
322
|
+
|
323
|
+
.and()
|
324
|
+
|
325
|
+
.logout()
|
326
|
+
|
327
|
+
.logoutSuccessUrl("/logout");
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
@Bean
|
336
|
+
|
337
|
+
PasswordEncoder passwordEncoder() {
|
338
|
+
|
339
|
+
return new BCryptPasswordEncoder(); //パスワードをハッシュ化する
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
/*このクラスは認証処理時に自動で呼ばれるクラス
|
348
|
+
|
349
|
+
やっていることは入力されたパスワードに対してBCryptでハッシュ化し、入力値が正しいか認証を行っている*/
|
350
|
+
|
351
|
+
@Configuration
|
352
|
+
|
353
|
+
protected static class AuthenticationConfiguration
|
354
|
+
|
355
|
+
extends GlobalAuthenticationConfigurerAdapter {
|
356
|
+
|
357
|
+
@Autowired
|
358
|
+
|
359
|
+
LoginUserDetailsService userDetailsService;
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
@Override
|
364
|
+
|
365
|
+
public void init(AuthenticationManagerBuilder auth) throws Exception {
|
366
|
+
|
367
|
+
auth.userDetailsService(userDetailsService)
|
368
|
+
|
369
|
+
.passwordEncoder(new BCryptPasswordEncoder());
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
```
|
380
|
+
|
381
|
+
|
382
|
+
|
221
383
|
ログインしているusersテーブルは
|
222
384
|
|
223
385
|
"${user.mail}"で取得できるので
|
1
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -218,6 +218,18 @@
|
|
218
218
|
|
219
219
|
|
220
220
|
|
221
|
+
ログインしているusersテーブルは
|
222
|
+
|
223
|
+
"${user.mail}"で取得できるので
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
やりたいことは
|
228
|
+
|
229
|
+
ログインしているユーザーの
|
230
|
+
|
231
|
+
プロフィールの情報を同じように得たいです
|
232
|
+
|
221
233
|
|
222
234
|
|
223
235
|
Securityのところで何か紐付けが必要なのでしょうか??
|