Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d657bc

Browse files
Initial Commit
1 parent bdcc722 commit 0d657bc

18 files changed

+509
-0
lines changed

‎pom.xml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.springframework.boot</groupId>
6+
<artifactId>spring-boot-starter-parent</artifactId>
7+
<version>1.5.4.RELEASE</version>
8+
</parent>
9+
<groupId>com.demo</groupId>
10+
<artifactId>jwt-spring-boot</artifactId>
11+
<version>0.0.1-SNAPSHOT</version>
12+
<name>jwt-spring-boot</name>
13+
<description>Integration Of Jwt with Spring Boot</description>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-tomcat</artifactId>
18+
<exclusions>
19+
<exclusion>
20+
<groupId>ch.qos.logback</groupId>
21+
<artifactId>logback-classic</artifactId>
22+
</exclusion>
23+
<exclusion>
24+
<groupId>org.slf4j</groupId>
25+
<artifactId>log4j-over-slf4j</artifactId>
26+
</exclusion>
27+
</exclusions>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-jersey</artifactId>
32+
<exclusions>
33+
<exclusion>
34+
<groupId>ch.qos.logback</groupId>
35+
<artifactId>logback-classic</artifactId>
36+
</exclusion>
37+
<exclusion>
38+
<groupId>org.slf4j</groupId>
39+
<artifactId>log4j-over-slf4j</artifactId>
40+
</exclusion>
41+
</exclusions>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-security</artifactId>
46+
<exclusions>
47+
<exclusion>
48+
<groupId>ch.qos.logback</groupId>
49+
<artifactId>logback-classic</artifactId>
50+
</exclusion>
51+
<exclusion>
52+
<groupId>org.slf4j</groupId>
53+
<artifactId>log4j-over-slf4j</artifactId>
54+
</exclusion>
55+
</exclusions>
56+
</dependency>
57+
<!-- logging API dependency slf4j and its implementation for log4j -->
58+
<dependency>
59+
<groupId>org.slf4j</groupId>
60+
<artifactId>slf4j-api</artifactId>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.slf4j</groupId>
64+
<artifactId>slf4j-log4j12</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.auth0</groupId>
68+
<artifactId>java-jwt</artifactId>
69+
<version>3.0.1</version>
70+
</dependency>
71+
</dependencies>
72+
<!-- build related parameters. Like, compiler version -->
73+
<build>
74+
<finalName>rest</finalName>
75+
<plugins>
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-compiler-plugin</artifactId>
79+
<configuration>
80+
<source>1.8</source>
81+
<target>1.8</target>
82+
</configuration>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.demo.jwt.auth;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
@SpringBootApplication
9+
public class ApplicationMain {
10+
11+
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationMain.class);
12+
13+
public static void main(String[] args) throws Exception{
14+
LOGGER.info("Starting JWT Authentication Application.");
15+
SpringApplication.run(ApplicationMain.class, args);
16+
LOGGER.info("JWT Authentication Application Started Successfully.");
17+
}
18+
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.demo.jwt.auth.config;
2+
3+
import org.glassfish.jersey.server.ResourceConfig;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import com.demo.jwt.auth.resource.UserResource;
7+
8+
@Configuration
9+
public class JerseyConfig extends ResourceConfig{
10+
11+
public JerseyConfig(){
12+
register(UserResource.class);
13+
}
14+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.demo.jwt.auth.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.autoconfigure.security.Http401AuthenticationEntryPoint;
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.EnableWebSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
10+
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
11+
12+
import com.demo.jwt.auth.filter.JWTAuthenticationFilter;
13+
import com.demo.jwt.auth.provider.JWTAuthenticationProvider;
14+
15+
@Configuration
16+
@EnableWebSecurity
17+
public class SecuriryConfig extends WebSecurityConfigurerAdapter{
18+
19+
@Autowired private JWTAuthenticationProvider jwtAuthenticationProvier;
20+
21+
@Override
22+
protected void configure(HttpSecurity http) throws Exception {
23+
http
24+
.authorizeRequests()
25+
.anyRequest().authenticated()
26+
.and()
27+
.addFilterBefore(new JWTAuthenticationFilter(), BasicAuthenticationFilter.class)
28+
.csrf().disable()
29+
.exceptionHandling().authenticationEntryPoint(new Http401AuthenticationEntryPoint(""));
30+
}
31+
32+
@Override
33+
public void configure(AuthenticationManagerBuilder authManagerBuilder){
34+
authManagerBuilder
35+
.authenticationProvider(jwtAuthenticationProvier);
36+
}
37+
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.demo.jwt.auth.dto;
2+
3+
import java.util.List;
4+
5+
public class UserResponseDto {
6+
7+
private String username;
8+
private List<?> roles;
9+
10+
public UserResponseDto(String username, List<?> roles){
11+
this.username=username;
12+
this.roles=roles;
13+
}
14+
15+
public String getUsername() {
16+
return username;
17+
}
18+
19+
public List<?> getRoles() {
20+
return roles;
21+
}
22+
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.demo.jwt.auth.exception;
2+
3+
import org.springframework.security.core.AuthenticationException;
4+
5+
public class BadJWTTokenException extends AuthenticationException{
6+
7+
private static final long serialVersionUID = 20170722155706L;
8+
9+
public BadJWTTokenException(String message){
10+
super(message);
11+
}
12+
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.demo.jwt.auth.filter;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.FilterChain;
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
import org.springframework.http.HttpHeaders;
11+
import org.springframework.security.core.context.SecurityContextHolder;
12+
import org.springframework.web.filter.OncePerRequestFilter;
13+
14+
import com.demo.jwt.auth.token.JWTAuthenticationToken;
15+
16+
public class JWTAuthenticationFilter extends OncePerRequestFilter {
17+
18+
@Override
19+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
20+
throws ServletException, IOException {
21+
String authHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
22+
if (authHeader != null) {
23+
String[] splitedAuthHeader = authHeader.split(" ");
24+
if (splitedAuthHeader.length == 2) {
25+
if (splitedAuthHeader[0].equalsIgnoreCase("Bearer")) {
26+
SecurityContextHolder.getContext()
27+
.setAuthentication(new JWTAuthenticationToken(splitedAuthHeader[1]));
28+
}
29+
}
30+
}
31+
filterChain.doFilter(request, response);
32+
}
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.demo.jwt.auth.impl;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.security.core.GrantedAuthority;
7+
import org.springframework.security.core.authority.AuthorityUtils;
8+
import org.springframework.security.core.userdetails.User;
9+
import org.springframework.stereotype.Component;
10+
11+
import com.auth0.jwt.JWTVerifier;
12+
import com.auth0.jwt.interfaces.DecodedJWT;
13+
import com.demo.jwt.auth.token.JWTTokenDetailsService;
14+
15+
@Component
16+
public class JWTTokenDetailsServiceImpl implements JWTTokenDetailsService {
17+
18+
@Autowired private JWTVerifier jwtVerifier;
19+
20+
@Override
21+
public User verify(String jwtToken) {
22+
DecodedJWT decodedJwt = jwtVerifier.verify(jwtToken);
23+
String role = decodedJwt.getClaim("rol").asString();
24+
String username = decodedJwt.getClaim("usr").asString();
25+
List<GrantedAuthority> authorityList=getAuthorityList(role);
26+
return new JWTUser(username,jwtToken,false,authorityList);
27+
}
28+
29+
private List<GrantedAuthority> getAuthorityList(String role){
30+
if(role != null){
31+
return AuthorityUtils.createAuthorityList("ROLE_"+role);
32+
}else{
33+
return AuthorityUtils.NO_AUTHORITIES;
34+
}
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.demo.jwt.auth.impl;
2+
3+
import java.util.Collection;
4+
5+
import org.springframework.security.core.GrantedAuthority;
6+
import org.springframework.security.core.userdetails.User;
7+
8+
public class JWTUser extends User{
9+
10+
private static final long serialVersionUID = 20170722140314l;
11+
12+
private String jwtToken;
13+
14+
public JWTUser(String username, String jwtToken, boolean isExpired, Collection<? extends GrantedAuthority> authorities){
15+
super(username,jwtToken,true,true,!isExpired,true,authorities);
16+
this.jwtToken=jwtToken;
17+
}
18+
19+
public String getJwtToken() {
20+
return jwtToken;
21+
}
22+
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.demo.jwt.auth.impl;
2+
3+
import java.io.UnsupportedEncodingException;
4+
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
import com.auth0.jwt.JWT;
9+
import com.auth0.jwt.JWTVerifier;
10+
import com.auth0.jwt.algorithms.Algorithm;
11+
12+
@Configuration
13+
public class JWTVendorConfig {
14+
15+
private static final String HMAC_KEY_SECRET = "d11da713ebeeaf256ba5adc5d21ec1b8fd1da68c99a4fb90cf17bd02d1f92ff1s";
16+
17+
@Bean
18+
public JWTVerifier jwtVerifier() throws IllegalArgumentException, UnsupportedEncodingException{
19+
Algorithm algorithm = Algorithm.HMAC256(HMAC_KEY_SECRET);
20+
JWTVerifier verifier = JWT.require(algorithm)
21+
.build();
22+
return verifier;
23+
}
24+
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /