Skip to content

Commit 4d2f4a0

Browse files
committed
spring安全
1 parent 0700b64 commit 4d2f4a0

File tree

8 files changed

+169
-0
lines changed

8 files changed

+169
-0
lines changed

security/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<groupId>org.springframework.boot</groupId>
2222
<artifactId>spring-boot-starter-security</artifactId>
2323
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
27+
</dependency>
2428
</dependencies>
2529

2630
<build>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package info.xiaomo.security;
2+
3+
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7+
import org.springframework.boot.autoconfigure.domain.EntityScan;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
11+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
12+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
13+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
14+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
15+
16+
/**
17+
* 把今天最好的表现当作明天最新的起点..~
18+
* いま 最高の表現 として 明日最新の始発..~
19+
* Today the best performance as tomorrow newest starter!
20+
* Created by IntelliJ IDEA.
21+
*
22+
* @author: xiaomo
23+
* @github: https://github.com/qq83387856
24+
* @email: hupengbest@163.com
25+
* @QQ_NO: 83387856
26+
* @Date: 2016/4/1 15:38
27+
* @Description: RabbitMq启动器
28+
* @Copyright(©) 2015 by xiaomo.
29+
**/
30+
@Configuration
31+
@EnableAutoConfiguration
32+
@ComponentScan("info.xiaomo")
33+
@EntityScan("info.xiaomo.*.model")
34+
@EnableJpaRepositories("info.xiaomo.*.dao")
35+
@EnableWebSecurity
36+
public class SecurityMain extends WebSecurityConfigurerAdapter {
37+
public static void main(String[] args) throws Exception {
38+
SpringApplication.run(SecurityMain.class, args);
39+
}
40+
41+
@Override
42+
protected void configure(HttpSecurity http) throws Exception {
43+
http.authorizeRequests()
44+
.antMatchers("/", "/home").permitAll()
45+
.anyRequest().authenticated()
46+
.and()
47+
.formLogin()
48+
.loginPage("/login")
49+
.permitAll()
50+
.and()
51+
.logout()
52+
.permitAll();
53+
}
54+
55+
@Autowired
56+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
57+
auth.inMemoryAuthentication()
58+
.withUser("user").password("password").roles("USER");
59+
}
60+
61+
62+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package info.xiaomo.security.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
7+
@Controller
8+
public class HelloController {
9+
10+
@RequestMapping("/")
11+
public String index() {
12+
return "index";
13+
}
14+
15+
@RequestMapping("/hello")
16+
public String hello() {
17+
return "hello";
18+
}
19+
20+
@RequestMapping(value = "/login", method = RequestMethod.GET)
21+
public String login() {
22+
return "login";
23+
}
24+
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
logging.config=classpath:config/logback-dev.xml
2+
server.port=8080
3+
server.session.timeout=1800
4+
server.max-http-header-size=20971520
5+
6+
#datasource
7+
spring.datasource.url=jdbc:mysql://115.29.137.34/xiaomo
8+
# ?useUnicode=true&amp;characterEncoding=UTF-8
9+
spring.datasource.username=xiaomo
10+
spring.datasource.password=123456
11+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
12+
13+
# \u914D\u7F6E\u8FD9\u4E2A\u503C\u5C31\u53EF\u4EE5\u683C\u5F0F\u5316\u65F6\u95F4
14+
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
15+
spring.jackson.time-zone=GMT+8
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<configuration scan="true">
4+
5+
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
6+
<encoder charset="UTF-8">
7+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</pattern>
8+
</encoder>
9+
</appender>
10+
11+
<root level="INFO">
12+
<appender-ref ref="stdout"/>
13+
</root>
14+
15+
<logger name="info.xiaomo" level="DEBUG"/>
16+
17+
</configuration>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
3+
>
4+
<head>
5+
<title>Hello World!</title>
6+
</head>
7+
<body>
8+
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
9+
<form th:action="@{/logout}" method="post">
10+
<input type="submit" value="注销"/>
11+
</form>
12+
</body>
13+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
3+
>
4+
<head>
5+
<title>Spring Security入门</title>
6+
</head>
7+
<body>
8+
<h1>欢迎使用Spring Security!</h1>
9+
10+
<p>点击 <a th:href="@{/hello}">这里</a> 打个招呼吧</p>
11+
</body>
12+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org"
4+
>
5+
<head>
6+
<title>Spring Security Example </title>
7+
</head>
8+
<body>
9+
<div th:if="${param.error}">
10+
用户名或密码错
11+
</div>
12+
<div th:if="${param.logout}">
13+
您已注销成功
14+
</div>
15+
<form th:action="@{/login}" method="post">
16+
<div><label> 用户名 : <input type="text" name="username"/> </label></div>
17+
<div><label> 密 码 : <input type="password" name="password"/> </label></div>
18+
<div><input type="submit" value="登录"/></div>
19+
</form>
20+
</body>
21+
</html>

0 commit comments

Comments
 (0)