Skip to content

Commit ca1aa58

Browse files
committed
SpringBoot之整合SpringSecurity
1 parent 52eb31e commit ca1aa58

File tree

660 files changed

+226834
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

660 files changed

+226834
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.yukong.mail;
2+
3+
/**
4+
* @author: yukong
5+
* @date: 2018/12/7 15:23
6+
*/
7+
public class ThreadTest {
8+
}

chapter12/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

chapter12/pom.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.4.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.yukong</groupId>
12+
<artifactId>springboot-springsecurity</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-springsecurity</name>
15+
<description>springboot-springsecurity study</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-security</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.mybatis.spring.boot</groupId>
32+
<artifactId>mybatis-spring-boot-starter</artifactId>
33+
<version>2.0.1</version>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>mysql</groupId>
38+
<artifactId>mysql-connector-java</artifactId>
39+
<scope>runtime</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.security</groupId>
48+
<artifactId>spring-security-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-maven-plugin</artifactId>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
62+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.yukong;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication
8+
@MapperScan("com.yukong.mapper")
9+
10+
public class SpringbootSpringsecurityApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(SpringbootSpringsecurityApplication.class, args);
14+
}
15+
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.yukong.config;
2+
3+
import com.yukong.mapper.UserMapper;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.security.core.userdetails.UserDetails;
6+
import org.springframework.security.core.userdetails.UserDetailsService;
7+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
8+
import org.springframework.stereotype.Service;
9+
10+
/**
11+
* @author yukong
12+
* @date 2019-04-11 17:35
13+
*/
14+
@Service
15+
public class MyUserDetailServiceImpl implements UserDetailsService {
16+
17+
@Autowired
18+
private UserMapper userMapper;
19+
20+
@Override
21+
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
22+
return userMapper.selectByUsername(username);
23+
}
24+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.yukong.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9+
import org.springframework.security.core.userdetails.UserDetailsService;
10+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
11+
import org.springframework.security.crypto.password.PasswordEncoder;
12+
13+
/**
14+
* @author yukong
15+
* @date 2019-04-11 15:08
16+
*/
17+
@Configuration
18+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
19+
20+
@Autowired
21+
private UserDetailsService userDetailsService;
22+
23+
// @Override
24+
// protected void configure(HttpSecurity http) throws Exception {
25+
// http.formLogin()
26+
// .loginPage("/panda-signIn.html")
27+
// .loginProcessingUrl("/authentication/form")
28+
// .failureUrl("/login?error")
29+
// .and()
30+
// .authorizeRequests()
31+
// .antMatchers(new String[]{"/static/**","/js/**","/css/**","/img/**","/images/**","/fonts/**","/**/favicon.ico"})
32+
// .permitAll()
33+
// .antMatchers("/panda-signIn.html")
34+
// .permitAll()
35+
// .anyRequest()
36+
// .authenticated()
37+
// .and()
38+
// .csrf().disable();
39+
// }
40+
41+
@Bean
42+
public PasswordEncoder passwordEncoder(){
43+
return new BCryptPasswordEncoder();
44+
}
45+
46+
@Autowired
47+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
48+
// 配置UserDetailsService 跟 PasswordEncoder 加密器
49+
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
50+
auth.eraseCredentials(false);
51+
}
52+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.yukong.controller;
2+
3+
import com.yukong.entity.User;
4+
import com.yukong.mapper.UserMapper;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
/**
11+
* @author yukong
12+
* @date 2019-04-11 15:22
13+
*/
14+
@RestController
15+
public class UserController {
16+
17+
@Autowired
18+
private UserMapper userMapper;
19+
20+
@RequestMapping("/user/{username}")
21+
public User hello(@PathVariable String username) {
22+
return userMapper.selectByUsername(username);
23+
}
24+
25+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.yukong.entity;
2+
3+
import org.springframework.security.core.GrantedAuthority;
4+
import org.springframework.security.core.authority.AuthorityUtils;
5+
import org.springframework.security.core.userdetails.UserDetails;
6+
7+
import java.util.Collection;
8+
9+
/**
10+
*
11+
* @author yukong
12+
* @date 2019-04-11 16:50
13+
*/
14+
public class User implements UserDetails {
15+
/**
16+
* 主键
17+
*/
18+
private Long id;
19+
20+
/**
21+
* 用户名
22+
*/
23+
private String username;
24+
25+
/**
26+
* 用户号码
27+
*/
28+
private String svcNum;
29+
30+
/**
31+
* 密码
32+
*/
33+
private String password;
34+
35+
/**
36+
* 客户id 1对1
37+
*/
38+
private Long custId;
39+
40+
public Long getId() {
41+
return id;
42+
}
43+
44+
public void setId(Long id) {
45+
this.id = id;
46+
}
47+
48+
@Override
49+
public String getUsername() {
50+
return username;
51+
}
52+
53+
@Override
54+
public boolean isAccountNonExpired() {
55+
return false;
56+
}
57+
58+
@Override
59+
public boolean isAccountNonLocked() {
60+
return false;
61+
}
62+
63+
@Override
64+
public boolean isCredentialsNonExpired() {
65+
return false;
66+
}
67+
68+
@Override
69+
public boolean isEnabled() {
70+
return true;
71+
}
72+
73+
public void setUsername(String username) {
74+
this.username = username;
75+
}
76+
77+
public String getSvcNum() {
78+
return svcNum;
79+
}
80+
81+
public void setSvcNum(String svcNum) {
82+
this.svcNum = svcNum;
83+
}
84+
85+
@Override
86+
public Collection<? extends GrantedAuthority> getAuthorities() {
87+
// 这里我们没有用到权限,所以返回一个默认的admin权限
88+
return AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
89+
}
90+
91+
@Override
92+
public String getPassword() {
93+
return password;
94+
}
95+
96+
public void setPassword(String password) {
97+
this.password = password;
98+
}
99+
100+
public Long getCustId() {
101+
return custId;
102+
}
103+
104+
public void setCustId(Long custId) {
105+
this.custId = custId;
106+
}
107+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.yukong.mapper;
2+
3+
import com.yukong.entity.User;
4+
5+
/**
6+
*
7+
* @author yukong
8+
* @date 2019-04-11 16:50
9+
*/
10+
public interface UserMapper {
11+
12+
int insertSelective(User record);
13+
14+
User selectByUsername(String username);
15+
16+
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring:
2+
datasource:
3+
driver-class-name: com.mysql.cj.jdbc.Driver
4+
url: jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=utf8
5+
username: root
6+
password: abc123
7+
mybatis:
8+
mapper-locations: classpath:mapper/*.xml

0 commit comments

Comments
 (0)