Skip to content

Commit ec6be5f

Browse files
authored
JWT Authentication demo
1 parent 2ab5d47 commit ec6be5f

29 files changed

+17553
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.3.RELEASE</version>
10+
<relativePath /> <!-- lookup parent from repository -->
11+
</parent>
12+
<groupId>com.alanbinu.fullstack.springboot.fullstack.basic.authentication</groupId>
13+
<artifactId>spring-boot-fullstack-basic-auth-login-logout</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>spring-boot-fullstack-basic-auth-login-logout</name>
16+
<description>Demo project for Spring Boot</description>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-security</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-devtools</artifactId>
36+
<scope>runtime</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-test</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.security</groupId>
45+
<artifactId>spring-security-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.in28minutes.fullstack.springboot.fullstack.basic.authentication.springbootfullstackbasicauthloginlogout;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootFullStackBasicAuthLoginLogoutApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootFullStackBasicAuthLoginLogoutApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.alanbinu.fullstack.springboot.fullstack.basic.authentication.springbootfullstackbasicauthloginlogout.basic.auth;
2+
public class AuthenticationBean {
3+
4+
private String message;
5+
6+
public AuthenticationBean(String message) {
7+
this.message = message;
8+
}
9+
10+
public String getMessage() {
11+
return message;
12+
}
13+
14+
public void setMessage(String message) {
15+
this.message = message;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return String.format("HelloWorldBean [message=%s]", message);
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.in28minutes.fullstack.springboot.fullstack.basic.authentication.springbootfullstackbasicauthloginlogout.basic.auth;
2+
import org.springframework.web.bind.annotation.CrossOrigin;
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
//Controller
7+
@CrossOrigin(origins={ "http://localhost:3000", "http://localhost:4200" })
8+
@RestController
9+
public class BasicAuthenticationController {
10+
11+
@GetMapping(path = "/basicauth")
12+
public AuthenticationBean helloWorldBean() {
13+
//throw new RuntimeException("Some Error has Happened! Contact Support at ***-***");
14+
return new AuthenticationBean("You are authenticated");
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.in28minutes.fullstack.springboot.fullstack.basic.authentication.springbootfullstackbasicauthloginlogout.basic.auth;
2+
import org.springframework.context.annotation.Configuration;
3+
import org.springframework.http.HttpMethod;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
7+
8+
@Configuration
9+
@EnableWebSecurity
10+
public class SpringSecurityConfigurationBasicAuth extends WebSecurityConfigurerAdapter{
11+
12+
@Override
13+
protected void configure(HttpSecurity http) throws Exception {
14+
http
15+
.csrf().disable()
16+
.authorizeRequests()
17+
.antMatchers(HttpMethod.OPTIONS,"/**").permitAll()
18+
.anyRequest().authenticated()
19+
.and()
20+
//.formLogin().and()
21+
.httpBasic();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.alanbinu.fullstack.springboot.fullstack.basic.authentication.springbootfullstackbasicauthloginlogout.course;
2+
3+
public class Course {
4+
private Long id;
5+
private String username;
6+
private String description;
7+
8+
public Course() {
9+
10+
}
11+
12+
public Course(long id, String username, String description) {
13+
super();
14+
this.id = id;
15+
this.username = username;
16+
this.description = description;
17+
}
18+
19+
public Long getId() {
20+
return id;
21+
}
22+
23+
public void setId(Long id) {
24+
this.id = id;
25+
}
26+
27+
public String getUsername() {
28+
return username;
29+
}
30+
31+
public void setUsername(String username) {
32+
this.username = username;
33+
}
34+
35+
public String getDescription() {
36+
return description;
37+
}
38+
39+
public void setDescription(String description) {
40+
this.description = description;
41+
}
42+
43+
@Override
44+
public int hashCode() {
45+
final int prime = 31;
46+
int result = 1;
47+
result = prime * result + ((description == null) ? 0 : description.hashCode());
48+
result = prime * result + ((id == null) ? 0 : id.hashCode());
49+
result = prime * result + ((username == null) ? 0 : username.hashCode());
50+
return result;
51+
}
52+
53+
@Override
54+
public boolean equals(Object obj) {
55+
if (this == obj)
56+
return true;
57+
if (obj == null)
58+
return false;
59+
if (getClass() != obj.getClass())
60+
return false;
61+
Course other = (Course) obj;
62+
if (description == null) {
63+
if (other.description != null)
64+
return false;
65+
} else if (!description.equals(other.description))
66+
return false;
67+
if (id == null) {
68+
if (other.id != null)
69+
return false;
70+
} else if (!id.equals(other.id))
71+
return false;
72+
if (username == null) {
73+
if (other.username != null)
74+
return false;
75+
} else if (!username.equals(other.username))
76+
return false;
77+
return true;
78+
}
79+
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.alanbinu.fullstack.springboot.fullstack.basic.authentication.springbootfullstackbasicauthloginlogout.course;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.CrossOrigin;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@CrossOrigin(origins = { "http://localhost:3000", "http://localhost:4200" })
12+
@RestController
13+
public class CourseResource {
14+
15+
@Autowired
16+
private CoursesHardcodedService courseManagementService;
17+
18+
@GetMapping("/instructors/{username}/courses")
19+
public List<Course> getAllCourses(@PathVariable String username) {
20+
return courseManagementService.findAll();
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.alanbinu.fullstack.springboot.fullstack.basic.authentication.springbootfullstackbasicauthloginlogout.course;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class CoursesHardcodedService {
10+
11+
private static List<Course> courses = new ArrayList<>();
12+
private static long idCounter = 0;
13+
14+
static {
15+
courses.add(new Course(++idCounter, "alanbinu", "Learn Full stack with Spring Boot and Angular"));
16+
courses.add(new Course(++idCounter, "alanbinu", "Learn Full stack with Spring Boot and React"));
17+
courses.add(new Course(++idCounter, "alanbinu", "Master Microservices with Spring Boot and Spring Cloud"));
18+
courses.add(new Course(++idCounter, "alanbinu",
19+
"Deploy Spring Boot Microservices to Cloud with Docker and Kubernetes"));
20+
}
21+
22+
public List<Course> findAll() {
23+
return courses;
24+
}
25+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.security.user.name=alanbinu
2+
spring.security.user.password=dummy

0 commit comments

Comments
 (0)