Spring Securityを使っています。"/auth/logout"というログアウト用URIにアクセスした際に、ログイン時にクッキーとして発行した認証用トークンを削除するようにしたいと思い、WebSecurityConfigurerAdapter継承クラスを以下ソースコードのように設定したのですが、Postmanを使って当該URIにアクセスすると以下エラーメッセージのように404エラーが発生してしまいます。
(追記)
ログイン用のURIである"/login"は、以下ソースコード(追記)のUsernamePasswordAuthenticationFilter継承クラスにおいてコンストラクタで指定しており、このURIへのリクエストは正常に作動します。
エラーメッセージ
"timestamp": "2022/07/12 13:17:22", "status": 404, "error": "Not Found", "message": "No message available", "path": "/login"
ソースコード
Java
1@Configuration 2public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { 3 4 @Autowired 5 MyUserDetailsService userDetailsService; 6 7 @Override 8 protected void configure(AuthenticationManagerBuilder auth) throws Exception { 9 auth.userDetailsService(this.userDetailsService) 10 .passwordEncoder(this.getPasswordEncoder()); 11 } 12 13 @Override 14 protected void configure(HttpSecurity http) throws Exception { 15 http.csrf().disable() 16 .cors().and() 17 .authorizeRequests() 18 .antMatchers("/auth/admin/**").hasRole("ADMIN") 19 .antMatchers("/auth/**").authenticated() 20 .anyRequest().permitAll() 21 .and() 22 //問題の箇所 23 .logout().logoutUrl("/auth/logout").deleteCookies("Authorization") 24 .addLogoutHandler(new MyLogoutHandler()).and() 25 .addFilter(new MyAuthenticationFilter(authenticationManagerBean() , getPasswordEncoder())) 26 .addFilter(new MyAuthorizationFilter(authenticationManagerBean() , userDetailsService)) 27 .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 28 } 29 30 @Override 31 @Bean 32 public AuthenticationManager authenticationManagerBean() throws Exception { 33 return super.authenticationManagerBean(); 34 } 35 36 @Bean 37 public PasswordEncoder getPasswordEncoder(){ 38 return new BCryptPasswordEncoder(); 39 } 40 41}
ソースコード(追記)
Java
1public class MyAuthenticationFilter extends UsernamePasswordAuthenticationFilter { 2 3 private AuthenticationManager authenticationManager; 4 private PasswordEncoder passwordEncoder; 5 6 public MyAuthenticationFilter(AuthenticationManager authenticationManager , PasswordEncoder passwordEncoder) { 7 8 this.authenticationManager = authenticationManager; 9 this.passwordEncoder = passwordEncoder; 10 // ログイン用URIの指定 11 this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login" , "POST")); 12 13 this.setUsernameParameter("email"); 14 this.setPasswordParameter("password"); 15 16 } 17}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/12 08:00 編集
2022/07/12 08:32