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

質問編集履歴

2

追加

2017/07/24 05:15

投稿

Yoshi--
Yoshi--

スコア62

title CHANGED
File without changes
body CHANGED
@@ -83,7 +83,7 @@
83
83
  ![イメージ説明](824c0bab8ba0bdde780e0614a2ca1f1e.png)
84
84
 
85
85
 
86
- Details
86
+ LoginUserDetails.java
87
87
  ```java
88
88
  //UserDetailsの実装クラス
89
89
 
@@ -108,6 +108,87 @@
108
108
 
109
109
  ```
110
110
 
111
+
112
+ SecurityConfig.java
113
+ ```java
114
+
115
+
116
+ package com.example.konkatsu;
117
+
118
+ import org.springframework.beans.factory.annotation.Autowired;
119
+ import org.springframework.context.annotation.Bean;
120
+ import org.springframework.context.annotation.Configuration;
121
+ import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
122
+ import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
123
+ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
124
+ import org.springframework.security.config.annotation.web.builders.WebSecurity;
125
+ import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
126
+ import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
127
+ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
128
+ import org.springframework.security.crypto.password.PasswordEncoder;
129
+
130
+ import com.example.konkatsu.service.LoginUserDetailsService;
131
+
132
+ @EnableWebSecurity //Spring Securityの基本的な設定(認証フィルタの設定など)が行われる
133
+ public class SecurityConfig extends WebSecurityConfigurerAdapter { //WebSecurityConfigurerAdapter を継承することでデフォルト設定に対して追加したい箇所だけオーバーライドして設定できる
134
+
135
+ @Override
136
+ public void configure(WebSecurity web) throws Exception { //configure(WebSecurity)メソッドをオーバーライドすることで特定のリクエストに対してセキュリティー設定を無視する設定など、全体に関わる設定ができる
137
+ web.ignoring().antMatchers("/webjars/**", "/css/**"); //「/webjars」「/css」といった静的リソースに対するアクセスにはセキュリティの設定は無視するようにする
138
+ }
139
+
140
+ @Override
141
+ protected void configure(HttpSecurity http) throws Exception { //configure(HttpSecurity)メソッドをオーバーライドすることで、認可の設定やログイン、ログアウトに関する設定ができる
142
+ http
143
+ .authorizeRequests() //認証が必要となるURLを設定
144
+ .antMatchers("/login").permitAll() //「/login」URLは認識不要。任意のユーザーがアクセスできるようにする
145
+ .antMatchers("/RegisterUser").permitAll()
146
+ .antMatchers("/createUser").permitAll()
147
+ .antMatchers("/createProfile").permitAll()
148
+ // .antMatchers("/konkatsu").permitAll()
149
+ .antMatchers("/RegisterUserForm").permitAll() //ユーザー登録は認識不要
150
+ .anyRequest().authenticated() //それ以外のパスには、認証なしでアクセスできないようにする
151
+
152
+
153
+
154
+ .and()
155
+ .formLogin().loginProcessingUrl("/login") //ログインに関する設定
156
+ .loginPage("/login") //ログインフォーム表示のパス(URL)
157
+ .failureUrl("/login?error") //認証失敗時の遷移先(?errorとつけておくとthymeleafの方でエラーのメッセージを出すときに便利)
158
+ .defaultSuccessUrl("/konkatsu", true) /*認証成功時の遷移先(第2引数のboolean
159
+ true : ログイン画面した後必ずtopにとばされる
160
+ false : (認証されてなくて一度ログイン画面に飛ばされても)ログインしたら指定したURLに飛んでくれる) */
161
+ .usernameParameter("mail").passwordParameter("pass") //メールアドレス、パスワードのパラメータ名を設定(ログイン画面のhtmlのinputのname属性を見に行っている)
162
+ .and()
163
+ .logout()
164
+ .logoutSuccessUrl("/logout");
165
+
166
+ }
167
+
168
+ @Bean
169
+ PasswordEncoder passwordEncoder() {
170
+ return new BCryptPasswordEncoder(); //パスワードをハッシュ化する
171
+ }
172
+
173
+
174
+ /*このクラスは認証処理時に自動で呼ばれるクラス
175
+ やっていることは入力されたパスワードに対してBCryptでハッシュ化し、入力値が正しいか認証を行っている*/
176
+ @Configuration
177
+ protected static class AuthenticationConfiguration
178
+ extends GlobalAuthenticationConfigurerAdapter {
179
+ @Autowired
180
+ LoginUserDetailsService userDetailsService;
181
+
182
+ @Override
183
+ public void init(AuthenticationManagerBuilder auth) throws Exception {
184
+ auth.userDetailsService(userDetailsService)
185
+ .passwordEncoder(new BCryptPasswordEncoder());
186
+ }
187
+ }
188
+ }
189
+
190
+ ```
191
+
111
192
  ログインしているusersテーブルは
112
193
  "${user.mail}"で取得できるので
113
194
 

1

追加

2017/07/24 05:15

投稿

Yoshi--
Yoshi--

スコア62

title CHANGED
File without changes
body CHANGED
@@ -108,5 +108,11 @@
108
108
 
109
109
  ```
110
110
 
111
+ ログインしているusersテーブルは
112
+ "${user.mail}"で取得できるので
111
113
 
114
+ やりたいことは
115
+ ログインしているユーザーの
116
+ プロフィールの情報を同じように得たいです
117
+
112
118
  Securityのところで何か紐付けが必要なのでしょうか??