JAVA-11240 Moved spring-cloud to spring-cloud-modules
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AuthServer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AuthServer.class, args);
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.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.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
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;
|
||||
|
||||
@Autowired
|
||||
private BCryptPasswordEncoder passwordEncoder;
|
||||
|
||||
@Override
|
||||
public void configure(
|
||||
ClientDetailsServiceConfigurer clients)
|
||||
throws Exception {
|
||||
clients
|
||||
.inMemory()
|
||||
.withClient("authserver")
|
||||
.secret(passwordEncoder.encode("passwordforauthserver"))
|
||||
.redirectUris("http://localhost:8080/login")
|
||||
.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;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("login").setViewName("login");
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
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.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@EnableOAuth2Client
|
||||
public class WebSecurityConfigurer
|
||||
extends
|
||||
WebSecurityConfigurerAdapter {
|
||||
|
||||
|
||||
@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(passwordEncoder().encode("user"))
|
||||
.roles("USER")
|
||||
.and()
|
||||
.withUser("admin").password("admin")
|
||||
.roles("USER", "ADMIN");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Bean(name = "userDetailsService")
|
||||
public UserDetailsService userDetailsServiceBean()
|
||||
throws Exception {
|
||||
return super.userDetailsServiceBean();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BCryptPasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
# Make the application available at http://localhost:7070/authserver
|
||||
server:
|
||||
port: 7070
|
||||
servlet:
|
||||
context-path: /authserver
|
||||
|
||||
# Our certificate settings for enabling JWT tokens
|
||||
jwt:
|
||||
certificate:
|
||||
store:
|
||||
file: classpath:/certificate/mykeystore.jks
|
||||
password: abirkhan04
|
||||
key:
|
||||
alias: myauthkey
|
||||
password: abirkhan04
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
+29
@@ -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