Skip to content

Commit

Permalink
suppl01: ユーザ認証(アカウント情報をハードコード)
Browse files Browse the repository at this point in the history
  • Loading branch information
teachingprogramming committed Jul 18, 2017
1 parent b7cb136 commit c503827
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -27,6 +27,7 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-security')
compile('com.h2database:h2')
// https://mvnrepository.com/artifact/commons-io/commons-io
compile group: 'commons-io', name: 'commons-io', version: '2.5'
Expand Down
@@ -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/");
}

}
@@ -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";
}

}
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Expand Up @@ -5,4 +5,5 @@ spring.datasource.url = jdbc:h2:~/webappdb/database
spring.datasource.username = sa
spring.datasource.password = sa
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=100MB
spring.http.multipart.max-request-size=100MB
security.basic.enabled=false
15 changes: 15 additions & 0 deletions src/main/resources/templates/suppl01/index.html
@@ -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>
20 changes: 20 additions & 0 deletions src/main/resources/templates/suppl01/login.html
@@ -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>
22 changes: 22 additions & 0 deletions src/main/resources/templates/suppl01/secret/index.html
@@ -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>
22 changes: 22 additions & 0 deletions src/main/resources/templates/suppl01/secret/info.html
@@ -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>

0 comments on commit c503827

Please sign in to comment.