前提・実現したいこと
ログイン成功時、権限ごとに遷移ページを変えたくてAutenticationSuccessHandlerを実装しました。
”MAKER"なら/maker/index.htmlへ
"ANSWERER"なら/answerer/index.htmlへ
遷移するようにしたいです。
発生している問題・エラーメッセージ
ログインしたら真っ白なページになってしまします。
該当のソースコード
Java
1@Configuration 2@EnableWebSecurity 3public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ 4 @Autowired 5 private SuccessHandler successHandler; 6 7 public void configure(WebSecurity web) { 8 web.ignoring().antMatchers("/css/**","/html/**"); 9 } 10 @Bean 11 SuccessHandler successHandler() { 12 return new SuccessHandler(); 13 } 14 15 16 @Override 17 protected void configure(HttpSecurity http) throws Exception { 18 http.authorizeRequests() 19 .antMatchers("/maker/**").hasRole("MAKER") 20 .antMatchers("/answerer/**").hasRole("ANSWERER") 21 .and() 22 .formLogin().loginPage("/login").successHandler(successHandler).permitAll() 23 .and() 24 .logout().logoutSuccessUrl("/"); 25 26 27 } 28 @Bean 29 public UserDetailsManager userDetailsManager(DataSource dataSource) { 30 return new JdbcUserDetailsManager(dataSource); 31 } 32 public class SuccessHandler implements AuthenticationSuccessHandler{ 33 34 @Override 35 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 36 Authentication authentication) throws IOException, ServletException { 37 Set<String> roles=AuthorityUtils.authorityListToSet(authentication.getAuthorities()); 38 if(roles.contains("MAKER")) { 39 response.sendRedirect(request.getContextPath()+"/maker"); 40 }else if(roles.contains("ANSWERER")) { 41 response.sendRedirect(request.getContextPath()+"/answerer"); 42 } 43 } 44 45 } 46}
Java
1@Controller 2@RequestMapping("/maker") 3public class MakerController { 4 @Autowired 5 private MakerService makerService; 6 7 @ModelAttribute("maker") 8 public Maker currentMaker(Account account) { 9 return account.getMaker(); 10 } 11 12 @GetMapping("") 13 public String index() { 14 return "maker/index"; 15 }
xml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.2.12.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>jp.kuroda.spring</groupId> 12 <artifactId>makeSurvey</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>makeSurvey</name> 15 <description>Demo project for Spring Boot</description> 16 <properties> 17 <java.version>1.8</java.version> 18 </properties> 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-data-jpa</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-security</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-thymeleaf</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-web</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>org.thymeleaf.extras</groupId> 38 <artifactId>thymeleaf-extras-springsecurity5</artifactId> 39 </dependency> 40 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-devtools</artifactId> 44 <scope>runtime</scope> 45 <optional>true</optional> 46 </dependency> 47 <dependency> 48 <groupId>com.h2database</groupId> 49 <artifactId>h2</artifactId> 50 <scope>runtime</scope> 51 </dependency> 52 <dependency> 53 <groupId>mysql</groupId> 54 <artifactId>mysql-connector-java</artifactId> 55 <scope>runtime</scope> 56 </dependency> 57 <dependency> 58 <groupId>org.projectlombok</groupId> 59 <artifactId>lombok</artifactId> 60 <optional>true</optional> 61 </dependency> 62 <dependency> 63 <groupId>org.springframework.boot</groupId> 64 <artifactId>spring-boot-starter-test</artifactId> 65 <scope>test</scope> 66 <exclusions> 67 <exclusion> 68 <groupId>org.junit.vintage</groupId> 69 <artifactId>junit-vintage-engine</artifactId> 70 </exclusion> 71 </exclusions> 72 </dependency> 73 <dependency> 74 <groupId>org.springframework.security</groupId> 75 <artifactId>spring-security-test</artifactId> 76 <scope>test</scope> 77 </dependency> 78 <dependency> 79 <groupId>nz.net.ultraq.thymeleaf</groupId> 80 <artifactId>thymeleaf-layout-dialect</artifactId> 81 </dependency> 82 </dependencies> 83 84 <build> 85 <plugins> 86 <plugin> 87 <groupId>org.springframework.boot</groupId> 88 <artifactId>spring-boot-maven-plugin</artifactId> 89 <configuration> 90 <excludes> 91 <exclude> 92 <groupId>org.projectlombok</groupId> 93 <artifactId>lombok</artifactId> 94 </exclude> 95 </excludes> 96 </configuration> 97 </plugin> 98 </plugins> 99 </build> 100 101</project>
補足情報(FW/ツールのバージョンなど)
開発環境は
Java 8
Spring Tool Suite 4
phpMyAdmin
サーバのバージョン: 10.4.17-MariaDB - mariadb.org binary distribution
アプリ開発初心者です。宜しくお願い致します。
あなたの回答
tips
プレビュー