1. Unnecessary file and code removed.
2. Project is relocated to spring-cloud folder
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
package com.cloudsecurity.auth;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class AuthServer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
SpringApplication.run(
|
||||
AuthServer.class, args);
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.cloudsecurity.auth.config;
|
||||
|
||||
import java.security.KeyPair;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
|
||||
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
|
||||
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
|
||||
|
||||
@Configuration
|
||||
@EnableAuthorizationServer
|
||||
@Order(6)
|
||||
public class AuthServerConfigurer
|
||||
extends
|
||||
AuthorizationServerConfigurerAdapter {
|
||||
|
||||
@Value("${jwt.certificate.store.file}")
|
||||
private Resource keystore;
|
||||
|
||||
@Value("${jwt.certificate.store.password}")
|
||||
private String keystorePassword;
|
||||
|
||||
@Value("${jwt.certificate.key.alias}")
|
||||
private String keyAlias;
|
||||
|
||||
@Value("${jwt.certificate.key.password}")
|
||||
private String keyPassword;
|
||||
|
||||
@Autowired
|
||||
private UserDetailsService userDetailsService;
|
||||
|
||||
@Override
|
||||
public void configure(
|
||||
ClientDetailsServiceConfigurer clients)
|
||||
throws Exception {
|
||||
clients
|
||||
.inMemory()
|
||||
.withClient("authserver")
|
||||
.secret("passwordforauthserver")
|
||||
.redirectUris("http://localhost:8080/")
|
||||
.authorizedGrantTypes("authorization_code",
|
||||
"refresh_token")
|
||||
.scopes("myscope")
|
||||
.autoApprove(true)
|
||||
.accessTokenValiditySeconds(30)
|
||||
.refreshTokenValiditySeconds(1800);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(
|
||||
AuthorizationServerEndpointsConfigurer endpoints)
|
||||
throws Exception {
|
||||
endpoints
|
||||
.accessTokenConverter(jwtAccessTokenConverter())
|
||||
.userDetailsService(userDetailsService);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JwtAccessTokenConverter jwtAccessTokenConverter() {
|
||||
KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(
|
||||
keystore, keystorePassword.toCharArray());
|
||||
KeyPair keyPair = keyStoreKeyFactory.getKeyPair(
|
||||
keyAlias, keyPassword.toCharArray());
|
||||
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
|
||||
converter.setKeyPair(keyPair);
|
||||
return converter;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.cloudsecurity.auth.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
||||
|
||||
/**
|
||||
* Our configuration for the OAuth2 User Info Resource Server.
|
||||
*/
|
||||
@Configuration
|
||||
@EnableResourceServer
|
||||
public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void configure(HttpSecurity http) throws Exception {
|
||||
http.antMatcher("/user")
|
||||
.authorizeRequests()
|
||||
.anyRequest()
|
||||
.authenticated();
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.cloudsecurity.auth.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("login").setViewName("login");
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.cloudsecurity.auth.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@EnableOAuth2Client
|
||||
public class WebSecurityConfigurer
|
||||
extends
|
||||
WebSecurityConfigurerAdapter {
|
||||
|
||||
// @Autowired
|
||||
// private OAuth2ClientContext oauth2ClientContext;
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http)
|
||||
throws Exception {
|
||||
http
|
||||
.authorizeRequests()
|
||||
.antMatchers("/login**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
.and().csrf()
|
||||
.and().formLogin().loginPage("/login");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(
|
||||
AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth
|
||||
.inMemoryAuthentication()
|
||||
.withUser("user").password("user")
|
||||
.roles("USER")
|
||||
.and()
|
||||
.withUser("admin").password("admin")
|
||||
.roles("USER", "ADMIN");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Bean(name = "userDetailsService")
|
||||
public UserDetailsService userDetailsServiceBean()
|
||||
throws Exception {
|
||||
return super.userDetailsServiceBean();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.cloudsecurity.auth.controller;
|
||||
|
||||
import java.security.Principal;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Because this application is also a User Info Resource Server, we expose info about the logged in user at:
|
||||
*
|
||||
* http://localhost:9090/auth/user
|
||||
*/
|
||||
@RestController
|
||||
public class ResourceController {
|
||||
|
||||
@RequestMapping("/user")
|
||||
public Principal user(Principal user) {
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
# Make the application available at http://localhost:7070/authserver
|
||||
server:
|
||||
port: 7070
|
||||
contextPath: /authserver
|
||||
|
||||
# Our certificate settings for enabling JWT tokens
|
||||
jwt:
|
||||
certificate:
|
||||
store:
|
||||
file: classpath:/certificate/mykeystore.jks
|
||||
password: abirkhan04
|
||||
key:
|
||||
alias: myauthkey
|
||||
password: abirkhan04
|
||||
|
||||
|
||||
security:
|
||||
oauth2:
|
||||
resource:
|
||||
filter-order: 3
|
||||
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,29 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>Baeldung Spring cloud Security</title>
|
||||
</head>
|
||||
<body onload="document.f.username.focus();">
|
||||
|
||||
<h1>Login</h1>
|
||||
|
||||
|
||||
<form th:action="@{/login}" name="f" method="post">
|
||||
<fieldset>
|
||||
<h2> Username and Password:</h2>
|
||||
<p>
|
||||
<label for="username">Username</label>
|
||||
<input type="text" id="username" name="username"/>
|
||||
</p>
|
||||
<p>
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" name="password"/>
|
||||
</p>
|
||||
<p>
|
||||
<input name="submit" type="submit" value="Login"/>
|
||||
</p>
|
||||
</fieldset>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user