Skip to content

Commit 7bc9d6f

Browse files
author
Ankur Singhal
committed
JWT Spring Integration
1 parent 3aa1137 commit 7bc9d6f

26 files changed

Lines changed: 774 additions & 4 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="output" path="target/classes"/>
31+
</classpath>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
target

‎jwt-spring-security-demo/.project‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>jwt-spring-security-demo</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/main/resources=UTF-8
4+
encoding/<project>=UTF-8
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

‎jwt-spring-security-demo/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+
6+
<groupId>nl.palmapps</groupId>
7+
<artifactId>jwt-spring-security-demo</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<!-- Inherit defaults from Spring Boot -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>1.3.3.RELEASE</version>
16+
</parent>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-actuator</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-security</artifactId>
36+
</dependency>
37+
38+
<!-- Test classes -->
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>io.swagger</groupId>
45+
<artifactId>swagger-core</artifactId>
46+
<version>1.5.8</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.jsonwebtoken</groupId>
50+
<artifactId>jjwt</artifactId>
51+
<version>0.6.0</version>
52+
</dependency>
53+
</dependencies>
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
</project>

‎jwt-spring-security-demo/readme.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Example project to show how to use JWT in combination with Spring Security to protect REST API endpoint.
2+
3+
Create token from JwtTokenGenerator
4+
Uncomment static main method
5+
6+
From Rest Client
7+
8+
URL - http://localhost:8888/hello?name=PalmApps
9+
GET
10+
11+
HEADER - Authorization
12+
Value - Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBbmt1ciBTaW5naGFsIiwidXNlcklkIjoiMTIzIiwicm9sZSI6ImFkbWluIn0.IquSGwNw788-ohDtkqmd3vYTyYbVol9Alm26GYSfFep5VRHsO7ZjIDoveifcbubrQU9X0YADF9ga17_xZwflvg
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package nl.palmapps.myawesomeproject;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
import static org.springframework.boot.SpringApplication.run;
6+
7+
/**
8+
* Starting point of the application
9+
*/
10+
@SpringBootApplication
11+
public class Application {
12+
13+
14+
public static void main(String[] args) {
15+
run(Application.class, args);
16+
}
17+
18+
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package nl.palmapps.myawesomeproject.controller;
2+
3+
import nl.palmapps.myawesomeproject.model.Greeting;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import java.security.Principal;
11+
import java.util.concurrent.atomic.AtomicLong;
12+
13+
/**
14+
* Example controller to test security calls
15+
*/
16+
@RestController
17+
public class MainController {
18+
19+
private static final String template = "Hello, %s!";
20+
private final AtomicLong counter = new AtomicLong();
21+
22+
23+
@RequestMapping(value = "/hello", method = RequestMethod.GET)
24+
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
25+
26+
return new Greeting(counter.incrementAndGet(),
27+
String.format(template, name));
28+
}
29+
30+
@RequestMapping(value = "/", method = RequestMethod.POST)
31+
public Greeting homePage(@RequestParam(value = "name", defaultValue = "World") String name) {
32+
33+
return new Greeting(counter.incrementAndGet(),
34+
String.format(template, name));
35+
}
36+
37+
@RequestMapping(value = {"/user", "/me"}, method = RequestMethod.POST)
38+
public ResponseEntity<?> user(Principal principal) {
39+
return ResponseEntity.ok(principal);
40+
}
41+
}

0 commit comments

Comments
 (0)