spring securityの画面遷移

実現したいこと

FUNCTION1は/user/listに、FUNCTION2は/user/employにそれぞれ画面遷移させたい。

前提

usernameとpasswordを正しく入力しても例外の処理にいってしまうので、それを直したい。おそらくFUNCTION1とFUNCTION2を正しく判定できていないのではないかと思います。よろしくお願いします。

発生している問題・エラーメッセージ

404エラー

該当のソースコード

java

1package com.example.demo.config;2 3import java.io.IOException;4 5import org.springframework.boot.autoconfigure.security.servlet.PathRequest;6import org.springframework.context.annotation.Bean;7import org.springframework.context.annotation.Configuration;8import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;9import org.springframework.security.config.annotation.web.builders.HttpSecurity;10import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;11import org.springframework.security.core.Authentication;12import org.springframework.security.core.userdetails.User;13import org.springframework.security.core.userdetails.UserDetails;14import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;15import org.springframework.security.crypto.password.PasswordEncoder;16import org.springframework.security.provisioning.InMemoryUserDetailsManager;17import org.springframework.security.web.SecurityFilterChain;18import org.springframework.security.web.authentication.AuthenticationSuccessHandler;19 20import jakarta.servlet.ServletException;21import jakarta.servlet.http.HttpServletRequest;22import jakarta.servlet.http.HttpServletResponse;23 24@Configuration25@EnableWebSecurity26@EnableMethodSecurity27public class SecurityConfig {28 29 @Bean30 public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {31 http 32 .authorizeHttpRequests(authz -> authz 33 .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()34 .requestMatchers("/").permitAll()35 .requestMatchers("/user/list").hasRole("FUNCTION1") // /user/listへのアクセスはFUNCTION1の役割が必要36 .requestMatchers("/user/employ").hasRole("FUNCTION2") // /user/employへのアクセスはFUNCTION2の役割が必要37 .anyRequest().authenticated()) // それ以外のリクエストは認証が必要38 .formLogin(formLogin -> formLogin 39 .successHandler(authenticationSuccessHandler())) // ログイン成功時のハンドラを設定40 .exceptionHandling(exceptionHandling -> exceptionHandling 41 .accessDeniedHandler((request, response, accessDeniedException) -> {42 response.sendRedirect("/");43 })); // アクセス拒否時のハンドラを設定44 return http.build();45 }46 47 @Bean48 public InMemoryUserDetailsManager userDetailsService() {49 // ユーザー1の設定50 UserDetails user1 = User.withUsername("user1")51 .passwordEncoder(passwordEncoder()::encode) // パスワードのエンコード52 .password("1111") // パスワード53 .roles("FUNCTION1") // ロール(役割)54 .build();55 56 // ユーザー2の設定57 UserDetails user2 = User.withUsername("user2")58 .passwordEncoder(passwordEncoder()::encode) // パスワードのエンコード59 .password("2222") // パスワード60 .roles("FUNCTION2") // ロール(役割)61 .build();62 63 return new InMemoryUserDetailsManager(user1, user2);64 }65 66 @Bean67 public PasswordEncoder passwordEncoder() {68 return new BCryptPasswordEncoder();69 }70 71 @Bean72 public AuthenticationSuccessHandler authenticationSuccessHandler() {73 return new CustomAuthenticationSuccessHandler();74 }75 76 // ログイン成功時のハンドラクラス77 public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {78 79 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,80 Authentication authentication) throws IOException, ServletException {81 if (request.isUserInRole("FUNCTION1")) {82 response.sendRedirect("/user/list"); // FUNCTION1の役割を持つユーザーは/user/listにリダイレクト83 } else if (request.isUserInRole("FUNCTION2")) {84 response.sendRedirect("/user/employ"); // FUNCTION2の役割を持つユーザーは/user/employにリダイレクト85 } else {86 response.sendRedirect("/"); // 上記以外のユーザーはルート(/)にリダイレクト87 }88 }89 }90}

試したこと

デバックを行い、下の方にあるCustomAuthenticationSuccessHandlerが呼び出せていない事がわかりました。これを動作させる方法がわかれば動作するのではないかと思っています。
よろしくお願いします。

コメントを投稿

0 コメント