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

質問編集履歴

1

Spring Securityの設定を追加しました。

2016/10/17 13:51

投稿

f97one
f97one

スコア29

title CHANGED
File without changes
body CHANGED
@@ -180,4 +180,59 @@
180
180
  testCompile('org.springframework.boot:spring-boot-starter-test')
181
181
  testCompile('org.springframework.security:spring-security-test')
182
182
  }
183
+ ```
184
+
185
+ ###追加事項
186
+
187
+ SpringSecurityの設定は以下のとおりです。
188
+ ログインしていない状態で、登録ボタンのロックを解除してsubmitしたところ、ログイン状態に応じて処理の振り分けができていることまでは、確認できています。
189
+
190
+ ```Java
191
+ @Configuration
192
+ @EnableWebSecurity
193
+ public class SecurityConfig extends WebSecurityConfigurerAdapter {
194
+
195
+ @Autowired
196
+ AuthorizedUsersService authorizedUserSvc;
197
+
198
+ @Override
199
+ protected void configure(HttpSecurity http) throws Exception {
200
+ http.authorizeRequests()
201
+ .antMatchers("/", "/loginForm", "/api/**", "/ledger/**").permitAll()
202
+ .antMatchers("/admin/**").hasRole(AppConstants.AUTHORITY_ADMIN)
203
+ .anyRequest().authenticated();
204
+
205
+ http.formLogin()
206
+ .loginProcessingUrl("/login")
207
+ .loginPage("/loginForm")
208
+ .failureUrl("/loginForm?error")
209
+ .defaultSuccessUrl("/", true)
210
+ .usernameParameter("username")
211
+ .passwordParameter("password")
212
+ .permitAll();
213
+
214
+ http.logout()
215
+ .logoutRequestMatcher(new AntPathRequestMatcher("/logout**"))
216
+ .logoutSuccessUrl("/");
217
+
218
+ }
219
+
220
+ @Override
221
+ public void configure(WebSecurity web) throws Exception {
222
+ web.ignoring().antMatchers("/webjars/**", "/css/**");
223
+ }
224
+
225
+ @Override
226
+ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
227
+ auth.authenticationProvider(createAuthProvider());
228
+ }
229
+
230
+ private AuthenticationProvider createAuthProvider() {
231
+ DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
232
+ provider.setUserDetailsService(authorizedUserSvc);
233
+ provider.setPasswordEncoder(new BCryptPasswordEncoder());
234
+
235
+ return provider;
236
+ }
237
+ }
183
238
  ```