Skip to content

Commit 0f7c5ae

Browse files
committed
single sign on
1 parent 568c56d commit 0f7c5ae

File tree

16 files changed

+387
-0
lines changed

16 files changed

+387
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
target
2+
out
3+
.settings
4+
.classpath
5+
.project
6+
.idea
7+
*.iml
8+
*.DS_Store
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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" 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+
<artifactId>hello-sso-jwt-auth</artifactId>
5+
<name>hello-sso-jwt-auth</name>
6+
<description>hello-sso-jwt-auth</description>
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>2.1.4.RELEASE</version>
11+
</parent>
12+
13+
<properties>
14+
<java.version>1.7</java.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-freemarker</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>io.jsonwebtoken</groupId>
28+
<artifactId>jjwt</artifactId>
29+
<version>0.9.1</version>
30+
</dependency>
31+
</dependencies>
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-maven-plugin</artifactId>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.hellokoding.sso.auth;
2+
3+
import org.springframework.web.util.WebUtils;
4+
5+
import javax.servlet.http.Cookie;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
9+
public class CookieUtil {
10+
public static void create(HttpServletResponse httpServletResponse, String name, String value, Boolean secure, Integer maxAge, String domain) {
11+
Cookie cookie = new Cookie(name, value);
12+
cookie.setSecure(secure);
13+
cookie.setHttpOnly(true);
14+
cookie.setMaxAge(maxAge);
15+
cookie.setDomain(domain);
16+
cookie.setPath("/");
17+
httpServletResponse.addCookie(cookie);
18+
}
19+
20+
public static void clear(HttpServletResponse httpServletResponse, String name) {
21+
Cookie cookie = new Cookie(name, null);
22+
cookie.setPath("/");
23+
cookie.setHttpOnly(true);
24+
cookie.setMaxAge(0);
25+
httpServletResponse.addCookie(cookie);
26+
}
27+
28+
public static String getValue(HttpServletRequest httpServletRequest, String name) {
29+
Cookie cookie = WebUtils.getCookie(httpServletRequest, name);
30+
return cookie != null ? cookie.getValue() : null;
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.hellokoding.sso.auth;
2+
3+
import io.jsonwebtoken.JwtBuilder;
4+
import io.jsonwebtoken.Jwts;
5+
import io.jsonwebtoken.SignatureAlgorithm;
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
import java.util.Date;
9+
10+
public class JwtUtil {
11+
public static String generateToken(String signingKey, String subject) {
12+
long nowMillis = System.currentTimeMillis();
13+
Date now = new Date(nowMillis);
14+
15+
JwtBuilder builder = Jwts.builder()
16+
.setSubject(subject)
17+
.setIssuedAt(now)
18+
.signWith(SignatureAlgorithm.HS256, signingKey);
19+
20+
return builder.compact();
21+
}
22+
23+
public static String getSubject(HttpServletRequest httpServletRequest, String jwtTokenCookieName, String signingKey){
24+
String token = CookieUtil.getValue(httpServletRequest, jwtTokenCookieName);
25+
if(token == null) return null;
26+
return Jwts.parser().setSigningKey(signingKey).parseClaimsJws(token).getBody().getSubject();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.hellokoding.sso.auth;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
8+
import javax.servlet.http.HttpServletResponse;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
@Controller
13+
public class LoginController {
14+
private static final String jwtTokenCookieName = "JWT-TOKEN";
15+
private static final String signingKey = "signingKey";
16+
private static final Map<String, String> credentials = new HashMap<>();
17+
18+
public LoginController() {
19+
credentials.put("hellokoding", "hellokoding");
20+
credentials.put("hellosso", "hellosso");
21+
}
22+
23+
@RequestMapping("/")
24+
public String home(){
25+
return "redirect:/login";
26+
}
27+
28+
@RequestMapping("/login")
29+
public String login(){
30+
return "login";
31+
}
32+
33+
@RequestMapping(value = "login", method = RequestMethod.POST)
34+
public String login(HttpServletResponse httpServletResponse, String username, String password, String redirect, Model model){
35+
if (username == null || !credentials.containsKey(username) || !credentials.get(username).equals(password)){
36+
model.addAttribute("error", "Invalid username or password!");
37+
return "login";
38+
}
39+
40+
String token = JwtUtil.generateToken(signingKey, username);
41+
CookieUtil.create(httpServletResponse, jwtTokenCookieName, token, false, -1, "localhost");
42+
43+
return "redirect:" + redirect;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.hellokoding.sso.auth;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class WebApplication{
8+
public static void main(String[] args) throws Exception {
9+
SpringApplication.run(WebApplication.class, args);
10+
}
11+
}
12+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Authentication Service</title>
5+
</head>
6+
<body>
7+
<form method="POST" action="/login?redirect=${RequestParameters.redirect!}">
8+
<h2>Log in</h2>
9+
<input name="username" type="text" placeholder="Username"
10+
autofocus="true"/>
11+
<input name="password" type="password" placeholder="Password"/>
12+
<div>(try username=hellokoding and password=hellokoding)</div>
13+
<div style="color: red">${error!}</div>
14+
<br/>
15+
<button type="submit">Log In</button>
16+
</form>
17+
</body>
18+
</html>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
target
2+
out
3+
.settings
4+
.classpath
5+
.project
6+
.idea
7+
*.iml
8+
*.DS_Store
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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" 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+
<artifactId>hello-sso-jwt-resource</artifactId>
5+
<name>hello-sso-jwt-resource</name>
6+
<description>hello-sso-jwt-resource</description>
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>2.1.4.RELEASE</version>
11+
</parent>
12+
13+
<properties>
14+
<java.version>1.7</java.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-freemarker</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>io.jsonwebtoken</groupId>
28+
<artifactId>jjwt</artifactId>
29+
<version>0.9.1</version>
30+
</dependency>
31+
</dependencies>
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-maven-plugin</artifactId>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.hellokoding.sso.resource;
2+
3+
import org.springframework.web.util.WebUtils;
4+
import javax.servlet.http.Cookie;
5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpServletResponse;
7+
8+
public class CookieUtil {
9+
public static void create(HttpServletResponse httpServletResponse, String name, String value, Boolean secure, Integer maxAge, String domain) {
10+
Cookie cookie = new Cookie(name, value);
11+
cookie.setSecure(secure);
12+
cookie.setHttpOnly(true);
13+
cookie.setMaxAge(maxAge);
14+
cookie.setDomain(domain);
15+
cookie.setPath("/");
16+
httpServletResponse.addCookie(cookie);
17+
}
18+
19+
public static void clear(HttpServletResponse httpServletResponse, String name) {
20+
Cookie cookie = new Cookie(name, null);
21+
cookie.setPath("/");
22+
cookie.setHttpOnly(true);
23+
cookie.setMaxAge(0);
24+
cookie.setDomain("localhost");
25+
httpServletResponse.addCookie(cookie);
26+
}
27+
28+
public static String getValue(HttpServletRequest httpServletRequest, String name) {
29+
Cookie cookie = WebUtils.getCookie(httpServletRequest, name);
30+
return cookie != null ? cookie.getValue() : null;
31+
}
32+
}
33+

0 commit comments

Comments
 (0)