質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Authentication

Authentication(認証)は正当性を認証する為の工程です。ログイン処理等で使われます。

Q&A

解決済

1回答

521閲覧

Spring boot でForm認証にしたいのに、Basic認証が出てしまう

AIUeno

総合スコア15

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Authentication

Authentication(認証)は正当性を認証する為の工程です。ログイン処理等で使われます。

0グッド

0クリップ

投稿2018/01/07 10:28

編集2018/02/14 01:22

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}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/01/07 16:17

クラス階層は正しいですか?
AIUeno

2018/01/08 01:32

正しくないようです。eclipseのデバックモードで、起動したところWebSecurityConfigが全く動作していないようです。マルチモジュール構成でクラスの読み込みが思ったように動作していないのが原因かもしれません。
guest

回答1

0

自己解決

実行クラスに
@SpringBootApplication(scanBasePackages = {"web", "admin.domain"})
を追加することで、Basic認証画面はでなくなりました。

しかし、別の子モジュールに入っている、依存パッケージのクラスが
@Autowiredできない問題で起動できなくなったので、別の質問としてあげたいと思います。

投稿2018/01/08 03:00

AIUeno

総合スコア15

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問