Commit 6674cb9d by linxu

支持JDK17; 重构

parent be9763ef
......@@ -3,180 +3,66 @@
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>com.keymobile.common</groupId>
<artifactId>loginService</artifactId>
<version>3.0.1</version>
<artifactId>login</artifactId>
<groupId>com.keymobile.sso</groupId>
<version>product-v2-rc1</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
<groupId>com.keymobile</groupId>
<artifactId>parent</artifactId>
<version>product-v1-1.0.4-rc1</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<auth.version>product-v2-1.0.3-rc1</auth.version>
<config.version>product-v1-1.0.4-rc1</config.version>
<crypto.version>product-v1-1.0.4-rc1</crypto.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>javax.interceptor</groupId>
<artifactId>javax.interceptor-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<groupId>com.keymobile.authservice</groupId>
<artifactId>common</artifactId>
<version>${auth.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<groupId>com.keymobile.authservice</groupId>
<artifactId>component</artifactId>
<version>${auth.version}</version>
</dependency>
<dependency>
<groupId>com.kakawait</groupId>
<artifactId>cas-security-spring-boot-starter</artifactId>
<version>1.0.0-beta-1</version>
<groupId>com.keymobile.authservice</groupId>
<artifactId>security</artifactId>
<version>${auth.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<groupId>com.keymobile</groupId>
<artifactId>config</artifactId>
<version>${config.version}</version>
</dependency>
<dependency>
<groupId>com.keymobile.auth</groupId>
<artifactId>security</artifactId>
<version>3.0.12-release</version>
<groupId>com.keymobile</groupId>
<artifactId>crypto</artifactId>
<version>${crypto.version}</version>
</dependency>
<dependency>
<groupId>com.keymobile</groupId>
<artifactId>config</artifactId>
<version>1.1.5-release</version>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-bom</artifactId>
<version>${spring-security.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>sso</finalName>
<resources>
<resource>
<directory>lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.yml</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>bootstrap.yml</exclude>
</excludes>
</resource>
</resources>
......@@ -184,11 +70,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>
-Xmx128M
</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
......
......@@ -4,14 +4,17 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.keymobile.login", "com.keymobile.config.logging"})
public class LoginApplication {
@ComponentScan(basePackages = {"com.keymobile.config.logging", "com.keymobile.config.naming",
"com.keymobile.config.redisclient"})
@PropertySource(value = "classpath:/application.yml")
public class SsoApplication {
public static void main(String[] args) {
SpringApplication.run(LoginApplication.class, args);
SpringApplication.run(SsoApplication.class, args);
}
}
package com.keymobile.login.api;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......
package com.keymobile.login.conf;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class RESTAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
public void commence(HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
......
package com.keymobile.login.conf;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
......@@ -18,4 +18,4 @@ public class RESTAuthenticationFailureHandler extends SimpleUrlAuthenticationFai
super.onAuthenticationFailure(request, response, exception);
}
}
}
\ No newline at end of file
package com.keymobile.login.conf;
import com.keymobile.login.logging.LogConstants;
import com.keymobile.login.logging.LogManager;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Component
public class RESTAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
@Value("${security.allowRootLogin:true}")
private boolean rootAllowLogin = true;
@Value("${security.allowRootLogin:true}")
private boolean rootAllowLogin = true;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
clearAuthenticationAttributes(request);
String returnStatus = "ok";
//check if allow root login
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
String userNameWithIdAttached = userDetails.getUsername();
LogManager.logInfo(LogConstants.CTX_API, userNameWithIdAttached + " 登录了系统");
if (userNameWithIdAttached.split(":")[0].equalsIgnoreCase("root")
&& !rootAllowLogin)
returnStatus = "root not allow login";
&& !rootAllowLogin)
returnStatus = "root not allow login";
PrintWriter writer = response.getWriter();
writer.write(returnStatus);
writer.flush();
......
package com.keymobile.login.conf;
import com.keymobile.auth.common.security.GrantedAuthHelper;
import com.keymobile.login.logging.LogConstants;
import com.keymobile.login.logging.LogManager;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class RESTLogoutSuccessHandler implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
if (authentication != null) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
String userNameWithIdAttached = userDetails != null ? userDetails.getUsername() : authentication.getName();
LogManager.logInfo(LogConstants.CTX_API, userNameWithIdAttached + " 退出了系统");
}
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.setStatus(HttpStatus.OK.value());
response.getWriter().flush();
}
}
}
\ No newline at end of file
package com.keymobile.login.conf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConf {
/**
* 注入 RedisConnectionFactory
*/
@Autowired
RedisConnectionFactory redisConnectionFactory;
/**
* 实例化 RedisTemplate 对象
*
* @return
*/
@Bean
public RedisTemplate<String, Object> functionDomainRedisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
return redisTemplate;
}
/**
* 设置数据存入 redis 的序列化方式
*
* @param redisTemplate
* @param factory
*/
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(factory);
}
/**
* 实例化 HashOperations 对象,可以使用 Hash 类型操作
*
* @param redisTemplate
* @return
*/
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
/**
* 实例化 ValueOperations 对象,可以使用 String 操作
*
* @param redisTemplate
* @return
*/
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
/**
* 实例化 ListOperations 对象,可以使用 List 操作
*
* @param redisTemplate
* @return
*/
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
/**
* 实例化 SetOperations 对象,可以使用 Set 操作
*
* @param redisTemplate
* @return
*/
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
/**
* 实例化 ZSetOperations 对象,可以使用 ZSet 操作
*
* @param redisTemplate
* @return
*/
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}}
package com.keymobile.login.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
@Configuration
class RedisSecurity {
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
}
package com.keymobile.login.conf;
import com.keymobile.auth.common.security.CustomizedUserDetailService;
import com.keymobile.authservice.component.CustomizedUserDetailService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import javax.sql.DataSource;
@Configuration
@ComponentScan("com.keymobile.auth.common.security")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ComponentScan("com.keymobile.authservice.component")
public class SecurityConfig {
@Autowired
private CustomizedUserDetailService customUserDetailService;
@Autowired
private RESTAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private RESTAuthenticationFailureHandler authenticationFailureHandler;
......@@ -26,13 +35,19 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private RESTLogoutSuccessHandler logoutSuccessHandler;
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserDetailService).passwordEncoder(NoOpPasswordEncoder.getInstance());
@Bean
public UserDetailsManager users(DataSource dataSource) {
return new CustomizedUserDetailService(dataSource);
}
@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
@Bean
protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll();
http.csrf().disable();
http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
......@@ -42,6 +57,22 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
http.formLogin().loginProcessingUrl("/signin");
http.logout().logoutUrl("/signout");
http.logout().logoutSuccessHandler(logoutSuccessHandler);
return http.build();
}
class SHA1PasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence charSequence) {
return DigestUtils.sha1Hex(charSequence.toString());
}
@Override
public boolean matches(CharSequence charSequence, String s) {
return DigestUtils.sha1Hex(charSequence.toString()).equals(s);
}
}
}
......@@ -2,6 +2,6 @@ package com.keymobile.login.logging;
public interface LogConstants {
String CTX_API = "sso.API";
String CTX_AUDIT = "sso.AUDIT";
}
spring:
session:
redis:
namespace: default
data:
redis:
host: redis0
port: 6379
password: password0
datasource:
hikari:
maximum-pool-size: 5
url: jdbc:mysql://mysql0:3306/d0?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSsl=false
username: user0
password: password0
driver-class-name: dm.jdbc.driver.DmDriver
eureka:
client:
registryFetchIntervalSeconds: 5
region: default
serviceUrl:
defaultZone: http://e0:8081/eureka/
enabled: true
\ No newline at end of file
server:
port: 8764
eureka:
client:
registerWithEureka: true
region: default
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: http://192.168.0.111:8081/eureka/
instance:
prefer-ip-address: false
hostname: 192.168.0.143
spring:
session:
store-type: redis
redis:
namespace: dataplatformdev
redis:
host: 192.168.0.192
port: 6379
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.0.192:3306/cgn2_dev?autoReconnect=true
username: test
password: test
security:
authUser: root
authPwd: pwd
permit: false
allowRootLogin: true
logging:
config: classpath:logback-custom.xml
\ No newline at end of file
jasypt.encryptor.algorithm=PBEWithMD5AndDES
jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator
\ No newline at end of file
server:
port: 8764
spring:
application:
name: auth
cloud:
openfeign:
httpclient:
connection-timeout: 1000
ok-http:
read-timeout: 5000
profiles:
active: default
management:
metrics:
tags:
application: ${spring.application.name}
endpoints:
health:
show-details: always
web:
exposure:
include: prometheus
logging:
level:
root: info
config: classpath:logback-custom.xml
spring:
profiles:
active: test
application:
name: auth
profiles:
active: local
cloud:
config:
uri:
\ No newline at end of file
uri: http://192.168.0.39:8082
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty name="spring.redis.host" source="spring.redis.host"/>
<springProperty name="spring.redis.port" source="spring.redis.port"/>
<springProperty name="redis.host" source="spring.data.redis.host"/>
<springProperty name="redis.port" source="spring.data.redis.port"/>
<springProperty name="redis.password" source="spring.data.redis.password"/>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %X{user} %X{session} %-5level %logger{5} - %msg%n</pattern>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{user} %X{session} %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<appender name="logstash" class="com.cwbase.logback.RedisAppender">
<host>${spring.redis.host}</host>
<port>${spring.redis.port}</port>
<appender name="logstash" class="com.keymobile.config.logging.RedisAppender">
<host>${redis.host}</host>
<port>${redis.port}</port>
<password>${redis.password}</password>
<key>logstash</key>
<additionalField>
<key>user</key>
<value>@{user}</value>
</additionalField>
<additionalField>
<key>session</key>
<value>@{session}</value>
</additionalField>
</appender>
<logger name="dataModeler.AUDIT">
<logger name="sso.AUDIT">
<appender-ref ref="logstash" />
</logger>
<root level="INFO">
<appender-ref ref="logstash" />
<appender-ref ref="stdout" />
</root>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment