-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
security 最简陋配置,加了一些从security教程中拿的页面代码
- Loading branch information
1 parent
867228e
commit 3b1ce81
Showing
11 changed files
with
173 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
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
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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/uetty/sample/springboot/controller/PageController.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,18 @@ | ||
package com.uetty.sample.springboot.controller; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import javax.websocket.server.PathParam; | ||
|
||
/** | ||
* 简陋的作为页面的controller | ||
*/ | ||
@Controller | ||
public class PageController extends BaseController { | ||
|
||
@RequestMapping("{pageName}") | ||
public String getPage(@PathParam("pageName") String pageName) { | ||
return pageName; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/uetty/sample/springboot/security/SecurityConfigure.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,60 @@ | ||
package com.uetty.sample.springboot.security; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Configurable; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.web.csrf.CsrfTokenRepository; | ||
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository; | ||
|
||
|
||
@EnableWebSecurity | ||
@AutoConfigureAfter(SecurityConfigure.SecurityBeanConfigure.class) | ||
public class SecurityConfigure extends WebSecurityConfigurerAdapter { | ||
|
||
@Configurable | ||
static class SecurityBeanConfigure { | ||
|
||
} | ||
|
||
@Value("${server.apiUrlPrefix}/login") | ||
private String loginProcessingUrl; | ||
@Value("${server.apiUrlPrefix}/**") | ||
private String apiPath; | ||
@Value("${server.apiUrlPrefix}/logout") | ||
private String logoutPath; | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
http.csrf().disable(); | ||
http | ||
.authorizeRequests() | ||
// api接口都需要登录 | ||
.antMatchers(apiPath).authenticated() | ||
// 其余路径(页面)不需要认证 | ||
.anyRequest().permitAll() | ||
|
||
.and() | ||
.formLogin() | ||
// 登录页面地址(也即判断请求未登录验证时,重定向到的页面) | ||
.loginPage("/login") | ||
// 登录接口路径 | ||
.loginProcessingUrl(loginProcessingUrl) | ||
// 登录成功后重定向到的页面地址 | ||
.defaultSuccessUrl("/index") | ||
// 登录失败时重定向到的页面 | ||
.failureUrl("/error") | ||
// 登录接口传递用户名的参数名 | ||
.usernameParameter("username") | ||
// 登录接口传递密码的参数名 | ||
.passwordParameter("password") | ||
.and() | ||
.logout() | ||
.logoutUrl(logoutPath) | ||
; | ||
} | ||
} |
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,13 @@ | ||
body { | ||
font-family: sans; | ||
font-size: 1em; | ||
} | ||
|
||
p.error { | ||
font-weight: bold; | ||
color: red; | ||
} | ||
|
||
div.logout { | ||
float: right; | ||
} |
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,24 @@ | ||
<!DOCTYPE html> | ||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5"> | ||
<head> | ||
<title>Hello Spring Security</title> | ||
<meta charset="utf-8" /> | ||
<link rel="stylesheet" href="/css/main.css" th:href="@{/css/main.css}" /> | ||
</head> | ||
<body> | ||
<div th:fragment="logout" class="logout" sec:authorize="isAuthenticated()"> | ||
Logged in user: <span sec:authentication="name"></span> | | ||
Roles: <span sec:authentication="principal.authorities"></span> | ||
<div> | ||
<form action="#" th:action="@{/api/logout}" method="post"> | ||
<input type="submit" value="Logout" /> | ||
</form> | ||
</div> | ||
</div> | ||
<h1>Hello Spring Security</h1> | ||
<p>This is an unsecured page, but you can access the secured pages after authenticating.</p> | ||
<ul> | ||
<li>Go to the <a href="/user/index" th:href="@{/user/index}">secured pages</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,21 @@ | ||
<!DOCTYPE html> | ||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"> | ||
<head> | ||
<title>Login page</title> | ||
<meta charset="utf-8" /> | ||
<link rel="stylesheet" href="/css/main.css" th:href="@{/css/main.css}" /> | ||
</head> | ||
<body> | ||
<h1>Login page</h1> | ||
<p>Example user: user / password</p> | ||
<p th:if="${loginError}" class="error">Wrong user or password</p> | ||
<form th:action="@{/api/login}" method="post"> | ||
<label for="username">Username</label>: | ||
<input type="text" id="username" name="username" autofocus="autofocus" /> <br /> | ||
<label for="password">Password</label>: | ||
<input type="password" id="password" name="password" /> <br /> | ||
<input type="submit" value="Log in" /> | ||
</form> | ||
<p><a href="/index" th:href="@{/index}">Back to home page</a></p> | ||
</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,13 @@ | ||
<!DOCTYPE html> | ||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"> | ||
<head> | ||
<title>Hello Spring Security</title> | ||
<meta charset="utf-8" /> | ||
<link rel="stylesheet" href="/css/main.css" th:href="@{/css/main.css}" /> | ||
</head> | ||
<body> | ||
<div th:substituteby="index::logout"></div> | ||
<h1>This is a secured page!</h1> | ||
<p><a href="/index" th:href="@{/index}">Back to home page</a></p> | ||
</body> | ||
</html> |