Skip to content

Commit 8aadc36

Browse files
authored
Merge pull request eugenp#3432 from abirkhan04/spring-cloud-security
Spring cloud security
2 parents 88b009b + 83c71dd commit 8aadc36

31 files changed

Lines changed: 956 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# README #
2+
3+
This README would normally document whatever steps are necessary to get your application up and running.
4+
5+
### What is this repository for? ###
6+
7+
* Quick summary
8+
* Version
9+
* [Learn Markdown](https://bitbucket.org/tutorials/markdowndemo)
10+
11+
### How do I get set up? ###
12+
13+
* Summary of set up
14+
* Configuration
15+
* Dependencies
16+
* Database configuration
17+
* How to run tests
18+
* Deployment instructions
19+
20+
### Contribution guidelines ###
21+
22+
* Writing tests
23+
* Code review
24+
* Other guidelines
25+
26+
### Who do I talk to? ###
27+
28+
* Repo owner or admin
29+
* Other community or team contact
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
myauthkey
2+
3+
4+
security:
5+
oauth2:
6+
resource:
7+
jwt:
8+
keyValue: |
9+
-----BEGIN PUBLIC KEY-----
10+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjj4JDMgT4OoaXisEd8Nz
11+
uiLwum9mh8BH1l9Atpe+uZkepf3Vnv0Bhxn0BGR+kYGwEHZPVpWsHEyTfIRdinaQ
12+
vlPaxWJquQW25yYstrCuQTKJvFjSO/cX/V4OGi1RUj76mOpwzkm1Kui3R7Sfh8Zo
13+
WO0GiWIFJqNBsZ9b1wOfBMXnge+A+u/qxVNnTFpwCVj6k2Yb4YUsmLNCmND7E3Ra
14+
BnrNQWqMU2numhV+ADpmVH08m/+pWdZ896uYu/tvQnz3agvZPcFsEst7LcNAWQFT
15+
eNLkfwVfepKWa9jPELemtTLf1MkMppU+Lj1UNCr8x4Y6EupRDZhplVNtqYsPNDpO
16+
7wIDAQAB
17+
-----END PUBLIC KEY-----
18+
19+
20+
21+
jwt:
22+
certificate:
23+
store:
24+
file: classpath:/certificate/my-auth-server.jks
25+
password: storepassword
26+
key:
27+
alias: myauthserver
28+
password: keypassword
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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>com.baeldung</groupId>
7+
<artifactId>auth-server</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.5.9.RELEASE</version>
15+
</parent>
16+
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-devtools</artifactId>
26+
<optional>true</optional>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-tomcat</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.cloud</groupId>
38+
<artifactId>spring-cloud-starter-oauth2</artifactId>
39+
<version>1.1.2.RELEASE</version>
40+
</dependency>
41+
</dependencies>
42+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.cloudsecurity.auth;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.*;
5+
6+
7+
@SpringBootApplication
8+
public class AuthServer {
9+
10+
public static void main(String[] args) {
11+
// TODO Auto-generated method stub
12+
SpringApplication.run(
13+
AuthServer.class, args);
14+
}
15+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.cloudsecurity.auth.config;
2+
3+
import java.security.KeyPair;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.core.annotation.Order;
10+
import org.springframework.core.io.Resource;
11+
import org.springframework.security.core.userdetails.UserDetailsService;
12+
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
13+
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
14+
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
15+
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
16+
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
17+
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
18+
19+
@Configuration
20+
@EnableAuthorizationServer
21+
@Order(6)
22+
public class AuthServerConfigurer
23+
extends
24+
AuthorizationServerConfigurerAdapter {
25+
26+
@Value("${jwt.certificate.store.file}")
27+
private Resource keystore;
28+
29+
@Value("${jwt.certificate.store.password}")
30+
private String keystorePassword;
31+
32+
@Value("${jwt.certificate.key.alias}")
33+
private String keyAlias;
34+
35+
@Value("${jwt.certificate.key.password}")
36+
private String keyPassword;
37+
38+
@Autowired
39+
private UserDetailsService userDetailsService;
40+
41+
@Override
42+
public void configure(
43+
ClientDetailsServiceConfigurer clients)
44+
throws Exception {
45+
clients
46+
.inMemory()
47+
.withClient("authserver")
48+
.secret("passwordforauthserver")
49+
.redirectUris("http://localhost:8080/")
50+
.authorizedGrantTypes("authorization_code",
51+
"refresh_token")
52+
.scopes("myscope")
53+
.autoApprove(true)
54+
.accessTokenValiditySeconds(30)
55+
.refreshTokenValiditySeconds(1800);
56+
}
57+
58+
@Override
59+
public void configure(
60+
AuthorizationServerEndpointsConfigurer endpoints)
61+
throws Exception {
62+
endpoints
63+
.accessTokenConverter(jwtAccessTokenConverter())
64+
.userDetailsService(userDetailsService);
65+
}
66+
67+
@Bean
68+
public JwtAccessTokenConverter jwtAccessTokenConverter() {
69+
KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(
70+
keystore, keystorePassword.toCharArray());
71+
KeyPair keyPair = keyStoreKeyFactory.getKeyPair(
72+
keyAlias, keyPassword.toCharArray());
73+
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
74+
converter.setKeyPair(keyPair);
75+
return converter;
76+
}
77+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.cloudsecurity.auth.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
6+
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
7+
8+
/**
9+
* Our configuration for the OAuth2 User Info Resource Server.
10+
*/
11+
@Configuration
12+
@EnableResourceServer
13+
public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
14+
15+
16+
17+
@Override
18+
public void configure(HttpSecurity http) throws Exception {
19+
http.antMatcher("/user")
20+
.authorizeRequests()
21+
.anyRequest()
22+
.authenticated();
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.cloudsecurity.auth.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
5+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
6+
7+
8+
@Configuration
9+
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
10+
11+
@Override
12+
public void addViewControllers(ViewControllerRegistry registry) {
13+
registry.addViewController("login").setViewName("login");
14+
}
15+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.cloudsecurity.auth.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
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+
11+
import org.springframework.security.core.userdetails.UserDetailsService;
12+
import org.springframework.security.oauth2.client.OAuth2ClientContext;
13+
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
14+
15+
@Configuration
16+
@EnableWebSecurity
17+
@EnableOAuth2Client
18+
public class WebSecurityConfigurer
19+
extends
20+
WebSecurityConfigurerAdapter {
21+
22+
23+
@Override
24+
protected void configure(HttpSecurity http)
25+
throws Exception {
26+
http
27+
.authorizeRequests()
28+
.antMatchers("/login**").permitAll()
29+
.anyRequest().authenticated()
30+
.and().csrf()
31+
.and().formLogin().loginPage("/login");
32+
}
33+
34+
@Override
35+
protected void configure(
36+
AuthenticationManagerBuilder auth) throws Exception {
37+
auth
38+
.inMemoryAuthentication()
39+
.withUser("user").password("user")
40+
.roles("USER")
41+
.and()
42+
.withUser("admin").password("admin")
43+
.roles("USER", "ADMIN");
44+
}
45+
46+
@Override
47+
@Bean(name = "userDetailsService")
48+
public UserDetailsService userDetailsServiceBean()
49+
throws Exception {
50+
return super.userDetailsServiceBean();
51+
}
52+
53+
54+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.cloudsecurity.auth.controller;
2+
3+
import java.security.Principal;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
/**
8+
* Because this application is also a User Info Resource Server, we expose info about the logged in user at:
9+
*
10+
* http://localhost:9090/auth/user
11+
*/
12+
@RestController
13+
public class ResourceController {
14+
15+
@RequestMapping("/user")
16+
public Principal user(Principal user) {
17+
return user;
18+
}
19+
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Make the application available at http://localhost:7070/authserver
2+
server:
3+
port: 7070
4+
contextPath: /authserver
5+
6+
# Our certificate settings for enabling JWT tokens
7+
jwt:
8+
certificate:
9+
store:
10+
file: classpath:/certificate/mykeystore.jks
11+
password: abirkhan04
12+
key:
13+
alias: myauthkey
14+
password: abirkhan04
15+
16+
17+
security:
18+
oauth2:
19+
resource:
20+
filter-order: 3
21+

0 commit comments

Comments
 (0)