-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0ab76db
Showing
14 changed files
with
476 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
target/ | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
nbproject/private/ | ||
build/ | ||
nbbuild/ | ||
dist/ | ||
nbdist/ | ||
.nb-gradle/ | ||
/data/ | ||
/target/ | ||
/mvnw | ||
/mvnw.cmd | ||
/.mvn/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
phpmyadmin: | ||
image: corbinu/docker-phpmyadmin | ||
ports : | ||
- "8082:80" | ||
environment: | ||
- MYSQL_USERNAME=root | ||
- MYSQL_PASSWORD=98U5mJY566 | ||
links: | ||
- database:mysql | ||
database: | ||
image: mysql:5.5 | ||
ports: | ||
- "3334:3306" | ||
environment: | ||
- MYSQL_ROOT_PASSWORD=98U5mJY566 | ||
- MYSQL_DATABASE=asfim | ||
- MYSQL_USER=asfim | ||
- MYSQL_PASSWORD=98U5mJY566 | ||
volumes: | ||
- ./data:/var/lib/mysql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>me.aboullaite</groupId> | ||
<artifactId>spring_boot_jwt</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring_boot_jwt</name> | ||
<description>Demo project for Spring Boot and JWT</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.1.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.jsonwebtoken</groupId> | ||
<artifactId>jjwt</artifactId> | ||
<version>0.6.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package me.aboullaite; | ||
|
||
import me.aboullaite.config.JwtFilter; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@SpringBootApplication | ||
public class SpringBootJwtApplication { | ||
|
||
@Bean | ||
public FilterRegistrationBean jwtFilter() { | ||
final FilterRegistrationBean registrationBean = new FilterRegistrationBean(); | ||
registrationBean.setFilter(new JwtFilter()); | ||
registrationBean.addUrlPatterns("/secure/*"); | ||
|
||
return registrationBean; | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringBootJwtApplication.class, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package me.aboullaite.config; | ||
|
||
|
||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
import org.springframework.web.filter.CorsFilter; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | ||
|
||
@Configuration | ||
public class CorsConfig { | ||
|
||
@Bean | ||
public FilterRegistrationBean corsFilter() { | ||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
CorsConfiguration config = new CorsConfiguration(); | ||
config.setAllowCredentials(true); | ||
config.addAllowedOrigin("*"); | ||
config.addAllowedHeader("*"); | ||
config.addAllowedMethod("OPTIONS"); | ||
config.addAllowedMethod("HEAD"); | ||
config.addAllowedMethod("GET"); | ||
config.addAllowedMethod("PUT"); | ||
config.addAllowedMethod("POST"); | ||
config.addAllowedMethod("DELETE"); | ||
config.addAllowedMethod("PATCH"); | ||
source.registerCorsConfiguration("/**", config); | ||
// return new CorsFilter(source); | ||
final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); | ||
bean.setOrder(0); | ||
return bean; | ||
} | ||
|
||
@Bean | ||
public WebMvcConfigurer mvcConfigurer() { | ||
return new WebMvcConfigurerAdapter() { | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**").allowedMethods("GET", "PUT", "POST", "GET", "OPTIONS"); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package me.aboullaite.config; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.web.filter.GenericFilterBean; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureException; | ||
|
||
public class JwtFilter extends GenericFilterBean { | ||
|
||
public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain) | ||
throws IOException, ServletException { | ||
|
||
final HttpServletRequest request = (HttpServletRequest) req; | ||
final HttpServletResponse response = (HttpServletResponse) res; | ||
final String authHeader = request.getHeader("authorization"); | ||
|
||
if ("OPTIONS".equals(request.getMethod())) { | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
|
||
chain.doFilter(req, res); | ||
} else { | ||
|
||
if (authHeader == null || !authHeader.startsWith("Bearer ")) { | ||
throw new ServletException("Missing or invalid Authorization header"); | ||
} | ||
|
||
final String token = authHeader.substring(7); | ||
|
||
try { | ||
final Claims claims = Jwts.parser().setSigningKey("secretkey").parseClaimsJws(token).getBody(); | ||
request.setAttribute("claims", claims); | ||
} catch (final SignatureException e) { | ||
throw new ServletException("Invalid token"); | ||
} | ||
|
||
chain.doFilter(req, res); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/me/aboullaite/controller/SecureController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package me.aboullaite.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import me.aboullaite.model.User; | ||
import me.aboullaite.service.UserService; | ||
|
||
@RestController | ||
@RequestMapping("/secure") | ||
public class SecureController { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@RequestMapping("/user/users") | ||
public String loginSuccess() { | ||
return "Login Successful!"; | ||
} | ||
|
||
@RequestMapping(value = "/user/email", method = RequestMethod.POST) | ||
public User findByEmail(@RequestBody String email) { | ||
return userService.findByEmail(email); | ||
} | ||
|
||
@RequestMapping(value = "/user/update", method = RequestMethod.POST) | ||
public User updateUser(@RequestBody User user) { | ||
return userService.save(user); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/me/aboullaite/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package me.aboullaite.controller; | ||
|
||
import java.util.Date; | ||
|
||
import javax.servlet.ServletException; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import me.aboullaite.model.User; | ||
import me.aboullaite.service.UserService; | ||
|
||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
|
||
@CrossOrigin(origins = "http://localhost", maxAge = 3600) | ||
@RestController | ||
@RequestMapping("/user") | ||
public class UserController { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@RequestMapping(value = "/register", method = RequestMethod.POST) | ||
public User registerUser(@RequestBody User user) { | ||
return userService.save(user); | ||
} | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.POST) | ||
public String login(@RequestBody User login) throws ServletException { | ||
|
||
String jwtToken = ""; | ||
|
||
if (login.getEmail() == null || login.getPassword() == null) { | ||
throw new ServletException("Please fill in username and password"); | ||
} | ||
|
||
String email = login.getEmail(); | ||
String password = login.getPassword(); | ||
|
||
User user = userService.findByEmail(email); | ||
|
||
if (user == null) { | ||
throw new ServletException("User email not found."); | ||
} | ||
|
||
String pwd = user.getPassword(); | ||
|
||
if (!password.equals(pwd)) { | ||
throw new ServletException("Invalid login. Please check your name and password."); | ||
} | ||
|
||
jwtToken = Jwts.builder().setSubject(email).claim("roles", "user").setIssuedAt(new Date()) | ||
.signWith(SignatureAlgorithm.HS256, "secretkey").compact(); | ||
|
||
return jwtToken; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package me.aboullaite.dao; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import me.aboullaite.model.User; | ||
|
||
@Repository | ||
public interface UserDao extends CrudRepository<User, Long> { | ||
User save(User user); | ||
|
||
User findByEmail(String email); | ||
} |
Oops, something went wrong.
Here are Two GETs.