Skip to content

Commit 9b6a286

Browse files
committed
first commit
0 parents  commit 9b6a286

19 files changed

+621
-0
lines changed

.gitignore

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

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# SpringBoot-Security-JWT

pom.xml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
6+
<groupId>com.pearl</groupId>
7+
<artifactId>springboot-jwt</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springboot-jwt</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.5.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-security</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.h2database</groupId>
39+
<artifactId>h2</artifactId>
40+
<scope>runtime</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>io.jsonwebtoken</groupId>
44+
<artifactId>jjwt</artifactId>
45+
<version>0.6.0</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-test</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.security</groupId>
54+
<artifactId>spring-security-test</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-devtools</artifactId>
60+
</dependency>
61+
</dependencies>
62+
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-maven-plugin</artifactId>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
72+
73+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.pearl.springbootjwt.Controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
@RequestMapping("/rest/hello")
9+
public class HelloController {
10+
11+
@GetMapping
12+
public String hello(){
13+
14+
return "Hello world!";
15+
}
16+
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.pearl.springbootjwt.Controller;
2+
3+
import com.pearl.springbootjwt.model.JwtUser;
4+
import com.pearl.springbootjwt.security.JwtGenerator;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
@RestController
8+
@RequestMapping("/token")
9+
public class TokenController {
10+
11+
private JwtGenerator jwtGenerator;
12+
13+
public TokenController(JwtGenerator jwtGenerator) {
14+
this.jwtGenerator = jwtGenerator;
15+
}
16+
17+
@PostMapping
18+
public String generate(@RequestBody final JwtUser jwtUser){
19+
20+
return jwtGenerator.generate(jwtUser);
21+
22+
}
23+
24+
@GetMapping("/{userName}")
25+
public String generate(@PathVariable final String username){
26+
27+
JwtGenerator jwtGenerator = new JwtGenerator();
28+
29+
// jwtGenerator.generate(username);
30+
31+
return null;
32+
33+
}
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.pearl.springbootjwt;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootJwtApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootJwtApplication.class, args);
11+
}
12+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.pearl.springbootjwt.config;
2+
3+
import com.pearl.springbootjwt.security.JwtAuthenticationEntryPoint;
4+
import com.pearl.springbootjwt.security.JwtAuthenticationProvider;
5+
import com.pearl.springbootjwt.security.JwtAuthenticationTokenFilter;
6+
import com.pearl.springbootjwt.security.JwtSuccessHandler;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.security.authentication.AuthenticationManager;
11+
import org.springframework.security.authentication.AuthenticationProvider;
12+
import org.springframework.security.authentication.ProviderManager;
13+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
14+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
15+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
16+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
17+
import org.springframework.security.config.http.SessionCreationPolicy;
18+
import org.springframework.security.web.AuthenticationEntryPoint;
19+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
20+
21+
import java.util.Collections;
22+
23+
@EnableGlobalMethodSecurity(prePostEnabled = true)
24+
@EnableWebSecurity
25+
@Configuration
26+
public class JwtSecurityConfig extends WebSecurityConfigurerAdapter {
27+
28+
@Autowired
29+
private JwtAuthenticationProvider authenticationProvider;
30+
31+
@Autowired
32+
private JwtAuthenticationEntryPoint entryPoint;
33+
34+
@Bean
35+
public AuthenticationManager authenticationManager(){
36+
return new ProviderManager(Collections.singletonList(authenticationProvider));
37+
}
38+
39+
@Bean
40+
public JwtAuthenticationTokenFilter authenticationTokenFilter(){
41+
42+
JwtAuthenticationTokenFilter filter = new JwtAuthenticationTokenFilter();
43+
filter.setAuthenticationManager(authenticationManager());
44+
filter.setAuthenticationSuccessHandler(new JwtSuccessHandler());
45+
46+
return filter;
47+
48+
}
49+
50+
@Override
51+
protected void configure(HttpSecurity http) throws Exception {
52+
http.csrf().disable()
53+
.authorizeRequests()
54+
.antMatchers("**/rest/")
55+
.authenticated()
56+
.and()
57+
.exceptionHandling()
58+
.authenticationEntryPoint(entryPoint)
59+
.and()
60+
.sessionManagement()
61+
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
62+
63+
http.addFilterBefore(authenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
64+
http.headers().cacheControl();
65+
}
66+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.pearl.springbootjwt.model;
2+
3+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
4+
5+
6+
public class JwtAuthenticationToken extends UsernamePasswordAuthenticationToken {
7+
8+
9+
private String token;
10+
11+
public JwtAuthenticationToken(String token) {
12+
super(null, null);
13+
this.token = token;
14+
}
15+
16+
public String getToken() {
17+
return token;
18+
}
19+
20+
public void setToken(String token) {
21+
this.token = token;
22+
}
23+
24+
@Override
25+
public Object getCredentials() {
26+
return null;
27+
}
28+
29+
@Override
30+
public Object getPrincipal() {
31+
return null;
32+
}
33+
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.pearl.springbootjwt.model;
2+
3+
public class JwtUser {
4+
5+
6+
private String userName;
7+
private long id;
8+
private String role;
9+
10+
public void setUserName(String userName) {
11+
this.userName = userName;
12+
}
13+
14+
public void setId(long id) {
15+
this.id = id;
16+
}
17+
18+
public void setRole(String role) {
19+
this.role = role;
20+
}
21+
22+
23+
public String getUserName() {
24+
return userName;
25+
}
26+
27+
public long getId() {
28+
return id;
29+
}
30+
31+
public String getRole() {
32+
return role;
33+
}
34+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.pearl.springbootjwt.model;
2+
3+
import org.springframework.security.core.GrantedAuthority;
4+
import org.springframework.security.core.userdetails.UserDetails;
5+
6+
import java.util.Collection;
7+
import java.util.List;
8+
9+
public class JwtUserDetails implements UserDetails {
10+
11+
12+
private String userName;
13+
private Long id;
14+
private String token;
15+
private List<GrantedAuthority> authorities;
16+
17+
public JwtUserDetails(String userName, long id, String token, List<GrantedAuthority> grantedAuthorities) {
18+
19+
20+
this.userName = userName;
21+
this.id = id;
22+
this.token = token;
23+
this.authorities = grantedAuthorities;
24+
}
25+
26+
27+
@Override
28+
public Collection<? extends GrantedAuthority> getAuthorities() {
29+
return null;
30+
}
31+
32+
@Override
33+
public String getPassword() {
34+
return null;
35+
}
36+
37+
@Override
38+
public String getUsername() {
39+
return userName;
40+
}
41+
42+
@Override
43+
public boolean isAccountNonExpired() {
44+
return true;
45+
}
46+
47+
@Override
48+
public boolean isAccountNonLocked() {
49+
return true;
50+
}
51+
52+
@Override
53+
public boolean isCredentialsNonExpired() {
54+
return true;
55+
}
56+
57+
@Override
58+
public boolean isEnabled() {
59+
return true;
60+
}
61+
62+
public String getUserName() {
63+
return userName;
64+
}
65+
66+
public void setUserName(String userName) {
67+
this.userName = userName;
68+
}
69+
70+
public Long getId() {
71+
return id;
72+
}
73+
74+
public void setId(Long id) {
75+
this.id = id;
76+
}
77+
78+
public String getToken() {
79+
return token;
80+
}
81+
82+
public void setToken(String token) {
83+
this.token = token;
84+
}
85+
86+
public void setAuthorities(List<GrantedAuthority> authorities) {
87+
this.authorities = authorities;
88+
}
89+
}

0 commit comments

Comments
 (0)