update 2021年11月4日12:09:56
parent
3faa725ec1
commit
f6ac9a62a8
|
|
@ -10,6 +10,18 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mystyle-cloud-admin-manager</artifactId>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
|
||||
|
|
@ -45,6 +57,11 @@
|
|||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-sleuth</artifactId>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,101 @@
|
|||
package com.zhangmeng.admin.manager.config.security;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
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;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
|
||||
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author 转身的背影在心底里沉沦
|
||||
* @date 2021年9月14日16:45:29
|
||||
* @version 1.0
|
||||
*/
|
||||
@Configuration
|
||||
@EnableResourceServer
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)// 激活方法上的PreAuthorize注解
|
||||
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
|
||||
|
||||
// 公钥
|
||||
private static final String PUBLIC_KEY = "public.key";
|
||||
|
||||
@Autowired
|
||||
private SecurityProperty securityProperty;
|
||||
|
||||
/***
|
||||
* 定义JwtTokenStore
|
||||
* @param jwtAccessTokenConverter
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public TokenStore tokenStore(JwtAccessTokenConverter jwtAccessTokenConverter) {
|
||||
return new JwtTokenStore(jwtAccessTokenConverter);
|
||||
}
|
||||
|
||||
/***
|
||||
* 定义JJwtAccessTokenConverter
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public JwtAccessTokenConverter jwtAccessTokenConverter() {
|
||||
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
|
||||
converter.setVerifierKey(getPubKey()); //秘钥的一部分
|
||||
return converter;
|
||||
}
|
||||
/**
|
||||
* 获取非对称加密公钥 Key
|
||||
* @return 公钥 Key
|
||||
*/
|
||||
private String getPubKey() {
|
||||
Resource resource = new ClassPathResource(PUBLIC_KEY);
|
||||
try {
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream());
|
||||
BufferedReader br = new BufferedReader(inputStreamReader);
|
||||
return br.lines().collect(Collectors.joining("\n"));
|
||||
} catch (IOException ioe) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* SpringSecurity
|
||||
* Http安全配置,对每个到达系统的http请求链接进行校验
|
||||
* @param http
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void configure(HttpSecurity http) throws Exception {
|
||||
|
||||
/**
|
||||
* - /swagger-ui.html
|
||||
* - /swagger-ui/**
|
||||
* - /swagger-resources/**
|
||||
* - /v2/api-docs
|
||||
* - /v3/api-docs
|
||||
* - /doc.html
|
||||
* - /webjars/springfox-swagger-ui/**
|
||||
*/
|
||||
|
||||
// 所有请求必须认证通过
|
||||
http.authorizeRequests()
|
||||
// 跨域预检请求
|
||||
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
|
||||
.antMatchers(securityProperty.getOpenApi()).permitAll()
|
||||
.anyRequest().
|
||||
authenticated(); // 其他地址需要认证授权
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package com.zhangmeng.admin.manager.config.security;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author 转身的背影在心底里沉沦
|
||||
* @date 2021年9月14日16:50:47
|
||||
* @version 1.0
|
||||
* */
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties("mystyle.security")
|
||||
public class SecurityProperty {
|
||||
|
||||
/**
|
||||
* 超级管理员不认证
|
||||
* */
|
||||
private boolean superAuthOpen;
|
||||
|
||||
/**
|
||||
* 不验证权限用户名
|
||||
* */
|
||||
private String superAdmin;
|
||||
|
||||
/**
|
||||
* 记住密码标识
|
||||
* */
|
||||
private String rememberKey;
|
||||
|
||||
/**
|
||||
* 开放接口列表
|
||||
* */
|
||||
private String[] openApi;
|
||||
|
||||
/**
|
||||
* 是否允许多账号在线
|
||||
* */
|
||||
private Integer maximum = 1;
|
||||
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ spring:
|
|||
type: web
|
||||
base-url: http://localhost:9411/
|
||||
service:
|
||||
name: mystyle-cloud-file
|
||||
name: mystyle-cloud-admin-manager
|
||||
sleuth:
|
||||
sampler:
|
||||
probability: 1
|
||||
|
|
@ -31,6 +31,9 @@ spring:
|
|||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
||||
feign:
|
||||
sentinel:
|
||||
enabled: true
|
||||
mybatis:
|
||||
type-aliases-package: com.zhangmeng.model.entity
|
||||
configuration:
|
||||
|
|
@ -40,4 +43,16 @@ mapper:
|
|||
style: normal
|
||||
enum-as-simple-type: true
|
||||
identity: MYSQL
|
||||
check-example-entity-class: true
|
||||
check-example-entity-class: true
|
||||
|
||||
mystyle:
|
||||
security:
|
||||
open-api:
|
||||
#swagger-ui.html
|
||||
- /swagger-ui.html
|
||||
- /swagger-ui/**
|
||||
- /swagger-resources/**
|
||||
- /v2/api-docs
|
||||
- /v3/api-docs
|
||||
- /doc.html
|
||||
- /webjars/**
|
||||
|
|
@ -0,0 +1 @@
|
|||
-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAizuj0fBV2+dj4lM3G6efKYvC2czd07BqmzV++E2yBguVks3XWvsW8qlzmG+t1XBCnRFDI/t1Ddc/Jsnlfy4YzRN8otb/Xn6Yz9ACFvZIPGx/q0cqcrgVaR9rSQiSzsGTgUGHNJk8r3A4w9PSSB552Z9s6p5TsWK5ezlfgg+2ANKn1eJ6R/hzajS/B1bTAqYcl9ddo7prneoeAN5LjlMhc2e0cSVgQt8ALP+4x/bTMnDkMjG6R8lnDAxE27B2ZPaLOIOjkUMK+9mZa4RNBoCDG6J/fwPD1NUoVRCbyr/TVaS4EzyhfNK1QW3BlZ0NLSI/SFD3eryKaFQdacJHS31neQIDAQAB-----END PUBLIC KEY-----
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
package com.zhangmeng.file.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
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;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
|
||||
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author 转身的背影在心底里沉沦
|
||||
* @date 2021年9月14日16:45:29
|
||||
* @version 1.0
|
||||
*/
|
||||
@Configuration
|
||||
@EnableResourceServer
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)// 激活方法上的PreAuthorize注解
|
||||
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
|
||||
|
||||
// 公钥
|
||||
private static final String PUBLIC_KEY = "public.key";
|
||||
|
||||
@Autowired
|
||||
private SecurityProperty securityProperty;
|
||||
|
||||
/***
|
||||
* 定义JwtTokenStore
|
||||
* @param jwtAccessTokenConverter
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public TokenStore tokenStore(JwtAccessTokenConverter jwtAccessTokenConverter) {
|
||||
return new JwtTokenStore(jwtAccessTokenConverter);
|
||||
}
|
||||
|
||||
/***
|
||||
* 定义JJwtAccessTokenConverter
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public JwtAccessTokenConverter jwtAccessTokenConverter() {
|
||||
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
|
||||
converter.setVerifierKey(getPubKey()); //秘钥的一部分
|
||||
return converter;
|
||||
}
|
||||
/**
|
||||
* 获取非对称加密公钥 Key
|
||||
* @return 公钥 Key
|
||||
*/
|
||||
private String getPubKey() {
|
||||
Resource resource = new ClassPathResource(PUBLIC_KEY);
|
||||
try {
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream());
|
||||
BufferedReader br = new BufferedReader(inputStreamReader);
|
||||
return br.lines().collect(Collectors.joining("\n"));
|
||||
} catch (IOException ioe) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* SpringSecurity
|
||||
* Http安全配置,对每个到达系统的http请求链接进行校验
|
||||
* @param http
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void configure(HttpSecurity http) throws Exception {
|
||||
|
||||
/**
|
||||
* - /swagger-ui.html
|
||||
* - /swagger-ui/**
|
||||
* - /swagger-resources/**
|
||||
* - /v2/api-docs
|
||||
* - /v3/api-docs
|
||||
* - /doc.html
|
||||
* - /webjars/springfox-swagger-ui/**
|
||||
*/
|
||||
|
||||
// 所有请求必须认证通过
|
||||
http.authorizeRequests()
|
||||
// 跨域预检请求
|
||||
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
|
||||
.antMatchers(securityProperty.getOpenApi()).permitAll()
|
||||
.anyRequest().
|
||||
authenticated(); // 其他地址需要认证授权
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package com.zhangmeng.file.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author 转身的背影在心底里沉沦
|
||||
* @date 2021年9月14日16:50:47
|
||||
* @version 1.0
|
||||
* */
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties("mystyle.security")
|
||||
public class SecurityProperty {
|
||||
|
||||
/**
|
||||
* 超级管理员不认证
|
||||
* */
|
||||
private boolean superAuthOpen;
|
||||
|
||||
/**
|
||||
* 不验证权限用户名
|
||||
* */
|
||||
private String superAdmin;
|
||||
|
||||
/**
|
||||
* 记住密码标识
|
||||
* */
|
||||
private String rememberKey;
|
||||
|
||||
/**
|
||||
* 开放接口列表
|
||||
* */
|
||||
private String[] openApi;
|
||||
|
||||
/**
|
||||
* 是否允许多账号在线
|
||||
* */
|
||||
private Integer maximum = 1;
|
||||
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package com.zhangmeng.file.controller;
|
|||
|
||||
import com.zhangmeng.api.service.file.UploadControllerApi;
|
||||
import com.zhangmeng.file.config.FileConfig;
|
||||
import com.zhangmeng.file.feign.UserFeignService;
|
||||
import com.zhangmeng.file.service.FileInfoService;
|
||||
import com.zhangmeng.file.service.UploadService;
|
||||
import com.zhangmeng.file.utils.Base64DecodeMultipartFile;
|
||||
|
|
|
|||
|
|
@ -1,14 +1,21 @@
|
|||
package com.zhangmeng.file.feign;
|
||||
|
||||
import com.zhangmeng.model.dto.SysConstant;
|
||||
import com.zhangmeng.model.entity.Permission;
|
||||
import com.zhangmeng.model.entity.User;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@FeignClient(value = "mystyle-cloud-user")
|
||||
@RequestMapping("/user")
|
||||
import java.util.List;
|
||||
|
||||
@FeignClient(SysConstant.mystyle_cloud_admin_manager)
|
||||
public interface UserFeignService {
|
||||
|
||||
@GetMapping("/username")
|
||||
public String hi(@RequestParam(value = "name", defaultValue = "forezp",required = false) String name);
|
||||
@RequestMapping("/user/findByUserName")
|
||||
public User findByUserName(@RequestParam(value = "username", defaultValue = "forezp",required = false) String username);
|
||||
|
||||
@RequestMapping("/permission/findByUserId")
|
||||
List<Permission> findByUserId(@RequestParam(value = "id", defaultValue = "forezp",required = false)Long id);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,4 +49,17 @@ file:
|
|||
url:
|
||||
aliyun: http://47.104.229.92
|
||||
localhost: localhost:8083
|
||||
vmware: http://192.168.52.165
|
||||
vmware: http://192.168.52.165
|
||||
mystyle:
|
||||
security:
|
||||
open-api:
|
||||
#swagger-ui.html
|
||||
- /swagger-ui.html
|
||||
- /swagger-ui/**
|
||||
- /swagger-resources/**
|
||||
- /v2/api-docs
|
||||
- /v3/api-docs
|
||||
- /doc.html
|
||||
- /webjars/**
|
||||
#
|
||||
- /upload/findByUserName
|
||||
|
|
@ -0,0 +1 @@
|
|||
-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAizuj0fBV2+dj4lM3G6efKYvC2czd07BqmzV++E2yBguVks3XWvsW8qlzmG+t1XBCnRFDI/t1Ddc/Jsnlfy4YzRN8otb/Xn6Yz9ACFvZIPGx/q0cqcrgVaR9rSQiSzsGTgUGHNJk8r3A4w9PSSB552Z9s6p5TsWK5ezlfgg+2ANKn1eJ6R/hzajS/B1bTAqYcl9ddo7prneoeAN5LjlMhc2e0cSVgQt8ALP+4x/bTMnDkMjG6R8lnDAxE27B2ZPaLOIOjkUMK+9mZa4RNBoCDG6J/fwPD1NUoVRCbyr/TVaS4EzyhfNK1QW3BlZ0NLSI/SFD3eryKaFQdacJHS31neQIDAQAB-----END PUBLIC KEY-----
|
||||
|
|
@ -99,5 +99,11 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-oauth2</artifactId>
|
||||
<version>2.2.4.RELEASE</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
package com.zhangmeng.model.bean;
|
||||
|
||||
import com.zhangmeng.model.dto.exception.CustomizeException;
|
||||
import com.zhangmeng.model.vo.Result;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.authentication.DisabledException;
|
||||
import org.springframework.security.authentication.LockedException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 全局异常处理
|
||||
*
|
||||
* @author zhangmeng
|
||||
* @version 1.0
|
||||
* @date 2021年4月25日10:47:56
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
/**
|
||||
* 处理自定义的业务异常
|
||||
* @param request
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = CustomizeException.class)
|
||||
public Result bizExceptionHandler(HttpServletRequest request, CustomizeException e){
|
||||
logger.error("发生业务异常!原因是:{}",e.getErrorMsg());
|
||||
return Result.error(e.getErrorCode(),e.getErrorMsg());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理登录业务异常
|
||||
* @param request
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = DisabledException.class)
|
||||
public Result bizExceptionHandler(HttpServletRequest request, DisabledException e){
|
||||
logger.error("发生业务异常!原因是:{}",e.getMessage());
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理登录业务异常
|
||||
* @param request
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = LockedException.class)
|
||||
public Result bizExceptionHandler(HttpServletRequest request, LockedException e){
|
||||
logger.error("发生业务异常!原因是:{}",e.getMessage());
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理空指针的异常
|
||||
* @param request
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value =NullPointerException.class)
|
||||
public Result exceptionHandler(HttpServletRequest request, NullPointerException e){
|
||||
logger.error("发生空指针异常!原因是:",e);
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.zhangmeng.model.dto;
|
||||
|
||||
/**
|
||||
* 系统常量
|
||||
*/
|
||||
public class SysConstant {
|
||||
|
||||
public static final String mystyle_cloud_admin_manager = "mystyle-cloud-admin-manager";
|
||||
|
||||
public static final String mystyle_cloud_oauth = "mystyle-cloud-oauth";
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package com.zhangmeng.model.dto.exception;
|
||||
|
||||
/**
|
||||
* @author zhangmeng
|
||||
* @version 1.0
|
||||
* @date 2021年1月22日 11:36:35
|
||||
* 自定义异常
|
||||
*/
|
||||
public class CustomizeException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = -4345234005653168721L;
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
private Integer errorCode;
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
private String errorMsg;
|
||||
|
||||
public CustomizeException() {
|
||||
super();
|
||||
}
|
||||
|
||||
//提供有参构造方法
|
||||
public CustomizeException(String errorMsg) {
|
||||
super(errorMsg);
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
public CustomizeException(Integer errorCode, String errorMsg) {
|
||||
super(errorMsg);
|
||||
this.errorCode = errorCode;
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
public CustomizeException(Integer errorCode, String errorMsg, Throwable cause) {
|
||||
super(errorMsg, cause);
|
||||
this.errorCode = errorCode;
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
|
||||
public Integer getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
public void setErrorCode(Integer errorCode) {
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public String getErrorMsg() {
|
||||
return errorMsg;
|
||||
}
|
||||
|
||||
public void setErrorMsg(String errorMsg) {
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Throwable fillInStackTrace() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
@ -38,6 +38,10 @@ public class Result<T> implements Serializable {
|
|||
return new Result(false,StatusCode.ERROR,message);
|
||||
}
|
||||
|
||||
public static Result error(Integer code,String message) {
|
||||
return new Result(false,code,message);
|
||||
}
|
||||
|
||||
public long getCount() {
|
||||
return count;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,18 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mystyle-cloud-oauth</artifactId>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@ package com.zhangmeng.oauth;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
* @author zhangmeng
|
||||
|
|
@ -13,6 +15,8 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
|||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
@EntityScan("com.zhangmeng.model.entity")
|
||||
@ComponentScan(basePackages = {"com.zhangmeng.oauth","com.zhangmeng.model.bean"})
|
||||
public class OauthApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OauthApplication.class,args);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.zhangmeng.oauth.config;
|
||||
|
||||
import com.zhangmeng.oauth.utils.UserJwt;
|
||||
import com.zhangmeng.oauth.dto.UserJwt;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.zhangmeng.oauth.config;
|
||||
|
||||
import com.zhangmeng.oauth.utils.JwtToken;
|
||||
import com.zhangmeng.oauth.dto.JwtToken;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package com.zhangmeng.oauth.config;
|
||||
|
||||
import com.zhangmeng.domain.admin.Permission;
|
||||
import com.zhangmeng.feign.admin.AdminFeign;
|
||||
import com.zhangmeng.oauth.utils.UserJwt;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.alibaba.cloud.commons.lang.StringUtils;
|
||||
import com.zhangmeng.model.entity.Permission;
|
||||
import com.zhangmeng.oauth.dto.UserJwt;
|
||||
import com.zhangmeng.oauth.feign.AdminFeign;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
|
@ -69,7 +69,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||
}
|
||||
|
||||
// 通过数据库去查询用户通过密码授权
|
||||
com.zhangmeng.domain.admin.User user = this.adminFeign.findByUserName(username);
|
||||
com.zhangmeng.model.entity.User user = this.adminFeign.findByUserName(username);
|
||||
|
||||
//根据用户查询权限列表
|
||||
List<Permission> permissions = this.adminFeign.findByUserId(user.getId());
|
||||
|
|
|
|||
|
|
@ -62,14 +62,6 @@ public class AuthController extends BaseController implements AuthControllerApi
|
|||
if (StringUtils.isEmpty(password)) {
|
||||
throw new RuntimeException("密码不允许为空");
|
||||
}
|
||||
User user = this.adminFeign.findByUserName(username);
|
||||
if (user == null){
|
||||
throw new UsernameNotFoundException("用户名错误");
|
||||
}
|
||||
//校验密码是否正确,如果正确则申请令牌
|
||||
if (!this.passwordEncoder.matches(password,user.getPassword())) {
|
||||
throw new RuntimeException("密码错误");
|
||||
}
|
||||
OauthConfig oauthConfig = this.oauthConfig();
|
||||
AuthToken authToken = auth_login(username, password,oauthConfig);
|
||||
//用户身份令牌
|
||||
|
|
|
|||
|
|
@ -1,12 +1,21 @@
|
|||
package com.zhangmeng.oauth.feign;
|
||||
|
||||
import com.zhangmeng.model.dto.SysConstant;
|
||||
import com.zhangmeng.model.entity.Permission;
|
||||
import com.zhangmeng.model.entity.User;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@FeignClient("")
|
||||
import java.util.List;
|
||||
|
||||
@FeignClient(SysConstant.mystyle_cloud_admin_manager)
|
||||
public interface AdminFeign {
|
||||
|
||||
@RequestMapping("")
|
||||
User findByUserName(String username);
|
||||
@RequestMapping("/user/findByUserName")
|
||||
public User findByUserName(@RequestParam(value = "username", defaultValue = "forezp",required = false) String username);
|
||||
|
||||
@RequestMapping("/permission/findByUserId")
|
||||
List<Permission> findByUserId(@RequestParam(value = "id", defaultValue = "forezp",required = false)Long id);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package com.zhangmeng.oauth.service.impl;
|
||||
|
||||
import com.zhangmeng.model.dto.SysConstant;
|
||||
import com.zhangmeng.model.dto.exception.CustomizeException;
|
||||
import com.zhangmeng.oauth.dto.AuthToken;
|
||||
import com.zhangmeng.oauth.service.AuthService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -20,6 +22,7 @@ import org.springframework.web.client.RestTemplate;
|
|||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@Service
|
||||
public class AuthServiceImpl implements AuthService {
|
||||
|
||||
|
|
@ -58,7 +61,7 @@ public class AuthServiceImpl implements AuthService {
|
|||
*/
|
||||
private AuthToken applyToken(String username, String password, String clientId, String clientSecret) {
|
||||
//选中认证服务的地址
|
||||
ServiceInstance serviceInstance = loadBalancerClient.choose("mystyle-user-oauth");
|
||||
ServiceInstance serviceInstance = loadBalancerClient.choose(SysConstant.mystyle_cloud_oauth);
|
||||
if (serviceInstance == null) {
|
||||
throw new RuntimeException("找不到对应的服务");
|
||||
}
|
||||
|
|
@ -96,7 +99,7 @@ public class AuthServiceImpl implements AuthService {
|
|||
}
|
||||
if(map == null || map.get("access_token") == null || map.get("refresh_token") == null || map.get("jti") == null) {
|
||||
//jti是jwt令牌的唯一标识作为用户身份令牌
|
||||
throw new RuntimeException("创建令牌失败!");
|
||||
throw new CustomizeException("创建令牌失败!");
|
||||
}
|
||||
|
||||
//将响应数据封装成AuthToken对象
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ server:
|
|||
port: 31006
|
||||
spring:
|
||||
application:
|
||||
name: mystyle-cloud-file
|
||||
name: mystyle-cloud-oauth
|
||||
datasource:
|
||||
username: root
|
||||
password: root
|
||||
|
|
@ -23,7 +23,7 @@ spring:
|
|||
type: web
|
||||
base-url: http://localhost:9411/
|
||||
service:
|
||||
name: mystyle-cloud-file
|
||||
name: mystyle-cloud-oauth
|
||||
sleuth:
|
||||
sampler:
|
||||
probability: 1
|
||||
|
|
@ -40,4 +40,16 @@ mapper:
|
|||
style: normal
|
||||
enum-as-simple-type: true
|
||||
identity: MYSQL
|
||||
check-example-entity-class: true
|
||||
check-example-entity-class: true
|
||||
encrypt:
|
||||
key-store:
|
||||
location: classpath:/mystyle-cloud.jks
|
||||
secret: mystyle-cloud
|
||||
alias: mystyle-cloud
|
||||
password: mystyle-cloud
|
||||
feign:
|
||||
client:
|
||||
config:
|
||||
default:
|
||||
connect-timeout: 20000
|
||||
read-timeout: 20000
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.zhangmeng.oauth;
|
||||
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
|
||||
public class BcryptTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
BCryptPasswordEncoder cryptPasswordEncoder = new BCryptPasswordEncoder();
|
||||
String encode = cryptPasswordEncoder.encode("mystyle-cloud");
|
||||
System.out.println(encode);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>mystyle-cloud-parent</artifactId>
|
||||
<groupId>com.zhangmeng</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mystyle-cloud-user</artifactId>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-sleuth</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
package com.zhangmeng.user;
|
||||
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* @author zhangmeng
|
||||
* @date 2021年10月30日14:55:11
|
||||
* @version 1.0
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
public class UserApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UserApplication.class,args);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
package com.zhangmeng.user.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@Value("${server.port}")
|
||||
String port;
|
||||
|
||||
@GetMapping("/username")
|
||||
public String hi(@RequestParam(value = "name", defaultValue = "forezp",required = false) String name) {
|
||||
return "hello " + name + ", i'm provider ,my port:" + port;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
server:
|
||||
port: 31001
|
||||
spring:
|
||||
application:
|
||||
name: mystyle-cloud-user
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
||||
sentinel:
|
||||
transport:
|
||||
port: 18763
|
||||
dashboard: localhost:8748
|
||||
zipkin:
|
||||
sender:
|
||||
type: web
|
||||
base-url: http://localhost:9411/
|
||||
service:
|
||||
name: mystyle-cloud-user
|
||||
sleuth:
|
||||
sampler:
|
||||
probability: 1
|
||||
|
||||
feign:
|
||||
sentinel:
|
||||
enabled: true
|
||||
Loading…
Reference in New Issue