Spring bootのログイン画面を
Form認証にしたいのですが、Basic認証画面がでてきてしまいます。
どこの設定が影響しているのかわかりません。
よろしくお願いします!
設定ファイル
xml
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-actuator</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-web</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>jp.co.test</groupId> 12 <artifactId>test</artifactId> 13 <version>${project.version}</version> 14 </dependency> 15 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-test</artifactId> 19 <scope>test</scope> 20 </dependency> 21 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-security</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.thymeleaf.extras</groupId> 28 <artifactId>thymeleaf-extras-springsecurity4</artifactId> 29 </dependency> 30 </dependencies>
Java
1@Configuration 2@EnableWebSecurity 3@EnableGlobalMethodSecurity(prePostEnabled = true) 4public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 5 6 @Autowired 7 AuthCustomerDetailsService customerDetailsService; 8 9 @Bean 10 public UserDetailsService userDetailsService() { 11 return super.userDetailsService(); 12 } 13 14 15 @Override 16 protected void configure(HttpSecurity http) throws Exception { 17 18 http.authorizeRequests() 19 .antMatchers("/login").permitAll() // ログイン画面 20 .anyRequest().authenticated(); // その他の全リクエストに対して認証を要求 21 22 http.formLogin() // 23 .loginPage("/login") 24 .usernameParameter("username") 25 .passwordParameter("password") 26 .successForwardUrl("/") // ログイン成功時に表示するURL 27 .failureUrl("/login?error=true").permitAll(); 28 29 http.logout() // 30 .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) // logoutUrl()はPOSTに対応していない 31 .logoutSuccessUrl("/login") // ログアウト成功時に表示するURL 32 .deleteCookies("JSESSIONID").invalidateHttpSession(true).permitAll(); 33 34 } 35 36 37 @Override 38 protected void configure(AuthenticationManagerBuilder auth) throws Exception { 39 auth.userDetailsService(customerDetailsService); 40 } 41 42 @Override 43 @Bean 44 public AuthenticationManager authenticationManagerBean() throws Exception { 45 return super.authenticationManagerBean(); 46 } 47 48 49}
ログインのコントローラー
Java
1import org.springframework.stereotype.Controller; 2import org.springframework.web.bind.annotation.RequestMapping; 3 4@Controller 5public class LoginController { 6 7 8 @RequestMapping("/login") 9 String login(){ 10 return "login/login"; 11 } 12}
ログイン画面のHTML
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<title>Login</title> 5<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" 6 href="../../css/bootstrap.min.css" /> 7</head> 8<body onload="document.f.username.focus();"> 9 <div class="container"> 10 <div class="navbar"> 11 <div class="navbar-inner"> 12 <a class="brand" href="http://www.thymeleaf.org"> Thymeleaf - 13 Plain </a> 14 <ul class="nav"> 15 <li><a th:href="@{/}" href="home.html"> Home </a></li> 16 </ul> 17 </div> 18 </div> 19 <div class="content"> 20 <p th:if="${param.logout}" class="alert">You have been logged out</p> 21 <p th:if="${param.error}" class="alert alert-error">There was an error, please try again</p> 22 <h2>Login with Username and Password</h2> 23 <form name="form" th:action="@{/login}" action="/login" method="POST"> 24 <fieldset> 25 <input type="text" name="username" value="" placeholder="Username" /> 26 <input type="password" name="password" placeholder="Password" /> 27 </fieldset> 28 <input type="submit" id="login" value="Login" 29 class="btn btn-primary" /> 30 </form> 31 </div> 32 </div> 33</body> 34</html>
※マルチモジュール構成になっています。
ディレクトリ構造は下記のとおりです。
引用テキスト
Package ←親プロジェクト
├── admin ←子モジュール
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
│ └── admin
│ ├── AdminApplication.java
│ ├── WebSecurityConfig.java
│ ├── app
│ │ └── MainController.java
│ ├── component
│ └── domain
│ ├── model
│ ├── repository
│ └── service
├── web ←子モジュール
├── pom.xml
├── src
├── main
├── java
└── web
├── WebApplication.java
├── WebSecurityConfig.java
├── app
│ └── WebMainController.java
└── service
WebApplication.java
Java
1package Package; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6@SpringBootApplication(scanBasePackages = {"web", "admin.domain"}) 7public class WebApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(WebApplication.class, args); 11 12 } 13 14}
回答1件
あなたの回答
tips
プレビュー