Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7cb136
commit c503827
Showing
8 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/net/teachingprogramming/webapp/SecurityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package net.teachingprogramming.webapp; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
|
||
@Configuration | ||
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { | ||
// アカウントの設定 | ||
auth.inMemoryAuthentication().withUser("admin").password("adminpassword").roles("ADMIN"); | ||
auth.inMemoryAuthentication().withUser("user1").password("user1password").roles("USER"); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
// 「/suppl01/secret/**」は認証が必要で、それ以外は認証が不要 | ||
http.authorizeRequests().antMatchers("/suppl01/secret/**").authenticated().anyRequest().permitAll(); | ||
|
||
// ログイン | ||
http.formLogin().loginPage("/suppl01/login").usernameParameter("username").passwordParameter("password") | ||
.loginProcessingUrl("/suppl01/loginProcess").defaultSuccessUrl("/suppl01/secret/").failureUrl("/suppl01/login?error"); | ||
|
||
// ログアウト | ||
http.logout().logoutUrl("/suppl01/logout").logoutSuccessUrl("/suppl01/"); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/net/teachingprogramming/webapp/Suppl01Controller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package net.teachingprogramming.webapp; | ||
|
||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.ModelMap; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
@Controller | ||
@RequestMapping("/suppl01") | ||
public class Suppl01Controller { | ||
|
||
/** | ||
* トップページ | ||
*/ | ||
@GetMapping("/") | ||
public String index() { | ||
return "suppl01/index"; | ||
} | ||
|
||
/** | ||
* ログインフォームを表示する | ||
*/ | ||
@GetMapping("/login") | ||
public String login() { | ||
return "suppl01/login"; | ||
} | ||
|
||
/** | ||
* ログイン後に遷移するページ | ||
*/ | ||
@GetMapping("/secret/") | ||
public String secretIndex() { | ||
return "suppl01/secret/index"; | ||
} | ||
|
||
/** | ||
* ログイン中のユーザの情報を表示するページ | ||
*/ | ||
@GetMapping("/secret/info") | ||
public String secretInfo(@AuthenticationPrincipal UserDetails userDetails, ModelMap modelMap) { | ||
modelMap.addAttribute("username", userDetails.getUsername()); | ||
return "suppl01/secret/info"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<title>トップ</title> | ||
</head> | ||
<body> | ||
<h1>Suppl01</h1> | ||
|
||
<ul> | ||
<li><a href="login">ログイン</a></li> | ||
</ul> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<title>ログイン</title> | ||
</head> | ||
<body> | ||
<h1>Suppl01</h1> | ||
<h2>ログイン</h2> | ||
<p th:if="${param.containsKey('error')}"> | ||
<strong style="color: red">ユーザ名またはパスワードが違います。</strong> | ||
</p> | ||
<form th:action="@{/suppl01/loginProcess}" method="post"> | ||
ユーザ名: <input name="username" /><br/> | ||
パスワード: <input name="password" type="password"/><br/> | ||
<button type="submit">ログイン</button> | ||
</form> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<title>Secret</title> | ||
</head> | ||
<body> | ||
<h1>Suppl01</h1> | ||
<h2>Secret</h2> | ||
<p> | ||
このページは、ログインしないと見られないページです。 | ||
</p> | ||
<ul> | ||
<li><a href="info">アカウント情報を表示</a></li> | ||
</ul> | ||
|
||
<form th:action="@{/suppl01/logout}" method="post"> | ||
<button type="submit">ログアウト</button> | ||
</form> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<title>Secret</title> | ||
</head> | ||
<body> | ||
<h1>Suppl01</h1> | ||
<h2>Secret</h2> | ||
<p> | ||
あなたのユーザ名は<span th:text="${username}">(ユーザ名)</span>です。 | ||
</p> | ||
<ul> | ||
<li><a href="/suppl01/secret/">戻る</a></li> | ||
</ul> | ||
|
||
<form th:action="@{/suppl01/logout}" method="post"> | ||
<button type="submit">ログアウト</button> | ||
</form> | ||
|
||
</body> | ||
</html> |