完成Druid封装,完成SwaggerUi封装
This commit is contained in:
87
src/main/java/com/common/base/config/SwaggerConfig.java
Normal file
87
src/main/java/com/common/base/config/SwaggerConfig.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package com.common.base.config;
|
||||
|
||||
import ch.qos.logback.classic.spi.ClassPackagingData;
|
||||
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
|
||||
import com.github.xiaoymin.knife4j.spring.filter.SecurityBasicAuthFilter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
@EnableKnife4j
|
||||
@Import(BeanValidatorPluginsConfiguration.class)
|
||||
public class SwaggerConfig extends WebMvcConfigurationSupport {
|
||||
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "swagger")
|
||||
public SwaggerEntity getSwaggerEntity() {
|
||||
return new SwaggerEntity();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
SwaggerEntity swaggerEntity = getSwaggerEntity();
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage(swaggerEntity.path))
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
.title("通用后台模板")
|
||||
.description("简单优雅的restfun风格")
|
||||
.termsOfServiceUrl("")
|
||||
.version("1.0")
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 防止@EnableMvc把默认的静态资源路径覆盖了,手动设置的方式
|
||||
*
|
||||
* @param registry
|
||||
*/
|
||||
@Override
|
||||
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
// 解决静态资源无法访问
|
||||
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
|
||||
// 解决swagger无法访问
|
||||
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
|
||||
// 解决swagger的js文件无法访问
|
||||
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean SwaggerFilterRegistration() {
|
||||
FilterRegistrationBean registration = new FilterRegistrationBean(new SecurityBasicAuthFilter());
|
||||
registration.addUrlPatterns("/*");
|
||||
registration.setName("swaggerSecurityBasic");
|
||||
SwaggerEntity swaggerEntity = getSwaggerEntity();
|
||||
registration.addInitParameter("enableBasicAuth",swaggerEntity.enableAuth);
|
||||
if (SwaggerEntity.TRUE.equals(swaggerEntity.enableAuth)) {
|
||||
registration.addInitParameter("userName",swaggerEntity.username);
|
||||
registration.addInitParameter("password",swaggerEntity.password);
|
||||
}
|
||||
return registration;
|
||||
}
|
||||
|
||||
}
|
||||
19
src/main/java/com/common/base/config/SwaggerEntity.java
Normal file
19
src/main/java/com/common/base/config/SwaggerEntity.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.common.base.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
@Data
|
||||
public class SwaggerEntity {
|
||||
|
||||
public static final String TRUE = "true";
|
||||
public static final String FALSE = "false";
|
||||
|
||||
public String path;
|
||||
|
||||
public String enableAuth = FALSE;
|
||||
|
||||
public String username = "admin";
|
||||
|
||||
public String password = "123456";
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package com.common.base.dao;
|
||||
|
||||
|
||||
import com.common.base.common.Mapper;
|
||||
import com.common.base.entity.FtToken;
|
||||
import com.common.base.shiro.FtToken;
|
||||
|
||||
/**
|
||||
* (FtToken)表数据库访问层
|
||||
|
||||
12
src/main/java/com/common/base/druid/DruidAuth.java
Normal file
12
src/main/java/com/common/base/druid/DruidAuth.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.common.base.druid;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DruidAuth {
|
||||
|
||||
private String username="admin";
|
||||
private String password="123456";
|
||||
private String allow = "";
|
||||
private String deny="";
|
||||
}
|
||||
62
src/main/java/com/common/base/druid/DruidConfig.java
Normal file
62
src/main/java/com/common/base/druid/DruidConfig.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package com.common.base.druid;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.http.StatViewServlet;
|
||||
import com.alibaba.druid.support.http.WebStatFilter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class DruidConfig implements WebMvcConfigurer {
|
||||
|
||||
@ConfigurationProperties(prefix = "spring.datasource")
|
||||
@Bean
|
||||
public DataSource druid(){
|
||||
return new DruidDataSource();
|
||||
}
|
||||
|
||||
@ConfigurationProperties(prefix = "druid")
|
||||
@Bean
|
||||
public DruidAuth auth() {
|
||||
return new DruidAuth();
|
||||
}
|
||||
|
||||
//配置Druid的监控
|
||||
//1、配置一个管理后台的Servlet
|
||||
@Bean
|
||||
public ServletRegistrationBean<StatViewServlet> statViewServlet(){
|
||||
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
|
||||
Map<String,String> initParams = new HashMap<>();
|
||||
DruidAuth druidAuth = auth();
|
||||
initParams.put("loginUsername",druidAuth.getUsername());
|
||||
initParams.put("loginPassword",druidAuth.getPassword());
|
||||
//默认就是允许所有访问
|
||||
initParams.put("allow",druidAuth.getAllow());
|
||||
initParams.put("deny",druidAuth.getDeny());
|
||||
bean.setInitParameters(initParams);
|
||||
return bean;
|
||||
}
|
||||
|
||||
|
||||
//2、配置一个web监控的filter
|
||||
@Bean
|
||||
public FilterRegistrationBean<WebStatFilter> webStatFilter(){
|
||||
FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>();
|
||||
bean.setFilter(new WebStatFilter());
|
||||
Map<String,String> initParams = new HashMap<>(1);
|
||||
initParams.put("exclusions","*.js,*.css,/druid/*");
|
||||
bean.setInitParameters(initParams);
|
||||
bean.setUrlPatterns(Collections.singletonList("/*"));
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.common.base.shiro;
|
||||
|
||||
|
||||
import com.common.base.entity.FtToken;
|
||||
import com.common.base.shiro.service.ShiroService;
|
||||
import com.common.base.shiro.service.SystemRealm;
|
||||
import com.common.base.utils.RedisUtil;
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.common.base.entity;
|
||||
package com.common.base.shiro;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
@@ -25,9 +23,6 @@ public class FtToken implements Serializable {
|
||||
private static final long serialVersionUID = 202774093136745829L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(
|
||||
strategy = GenerationType.IDENTITY,
|
||||
generator = "select TOKEN_ID.nextval from dual")
|
||||
private String tokenId;
|
||||
|
||||
private String token;
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.common.base.shiro;
|
||||
|
||||
import com.common.base.shiro.service.SystemRealm;
|
||||
import com.common.base.utils.SpringContextUtil;
|
||||
import org.apache.shiro.mgt.SecurityManager;
|
||||
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
|
||||
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
|
||||
@@ -9,18 +8,11 @@ import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
||||
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
||||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.Filter;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
|
||||
@@ -2,14 +2,16 @@ package com.common.base.shiro.service;
|
||||
|
||||
|
||||
import com.common.base.common.ResultVO;
|
||||
import com.common.base.entity.FtToken;
|
||||
import com.common.base.shiro.FtToken;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author lhc
|
||||
*/
|
||||
public interface ShiroService {
|
||||
|
||||
ResultVO createToken(String userId);
|
||||
ResultVO createToken(String userId,String token, Date expireTime);
|
||||
|
||||
ResultVO logout(String accessToken);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.common.base.shiro.service;
|
||||
import com.common.base.common.ResultVO;
|
||||
import com.common.base.common.ServiceException;
|
||||
import com.common.base.dao.FtTokenDao;
|
||||
import com.common.base.entity.FtToken;
|
||||
import com.common.base.shiro.FtToken;
|
||||
import com.common.base.utils.RedisUtil;
|
||||
import com.common.base.utils.TokenProccessor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -45,21 +45,16 @@ public class ShiroServiceImpl implements ShiroService {
|
||||
*@param [userId]
|
||||
*@return Result
|
||||
*/
|
||||
public ResultVO createToken(String userId) {
|
||||
public ResultVO createToken(String userId,String token,Date expireTime) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
TokenProccessor tokenProccessor = TokenProccessor.getInstance();
|
||||
//生成一个token
|
||||
String token = tokenProccessor.makeToken();
|
||||
//当前时间
|
||||
Date now = new Date();
|
||||
//过期时间
|
||||
Date expireTime = new Date(now.getTime() + EXPIRE);
|
||||
|
||||
FtToken tokenEntity = new FtToken();
|
||||
Date now = new Date();
|
||||
// 是否使用redis存入token
|
||||
if (RedisUtil.isUseful) {
|
||||
boolean flag = redisUtil.hset("session", String.valueOf(userId), token,EXPIRE / 1000);
|
||||
if (flag) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
Map<String, Object> map = new HashMap<>(2);
|
||||
map.put("userId", userId);
|
||||
map.put("expireTime",expireTime);
|
||||
flag = redisUtil.hmset(token, map, EXPIRE / 1000);
|
||||
@@ -75,6 +70,7 @@ public class ShiroServiceImpl implements ShiroService {
|
||||
tokenEntity = tokenMapper.selectOne(tokenEntity);
|
||||
if (tokenEntity == null) {
|
||||
tokenEntity = new FtToken();
|
||||
tokenEntity.setTokenId(UUID.randomUUID().toString()+System.currentTimeMillis());
|
||||
tokenEntity.setUserId(userId);
|
||||
tokenEntity.setToken(token);
|
||||
tokenEntity.setUpdateTime(now);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.common.base.shiro.service;
|
||||
|
||||
|
||||
import com.common.base.entity.FtToken;
|
||||
import org.apache.shiro.authz.SimpleAuthorizationInfo;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -33,9 +32,4 @@ public interface SystemRealm {
|
||||
*/
|
||||
LinkedHashMap<String, String> setShiroUrl();
|
||||
|
||||
/**
|
||||
* 设置Shiro的token
|
||||
* @return token
|
||||
*/
|
||||
FtToken setShiroToken();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.common.base.utils;
|
||||
|
||||
import com.common.base.entity.FtToken;
|
||||
import com.common.base.shiro.FtToken;
|
||||
import com.common.base.shiro.service.ShiroService;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.common.base.dao.FtTokenDao">
|
||||
<resultMap type="com.common.base.entity.FtToken" id="FtTokenMap">
|
||||
<resultMap type="com.common.base.shiro.FtToken" id="FtTokenMap">
|
||||
<result property="tokenId" column="TOKEN_ID" jdbcType="INTEGER"/>
|
||||
<result property="token" column="TOKEN" jdbcType="VARCHAR"/>
|
||||
<result property="userId" column="USER_ID" jdbcType="INTEGER"/>
|
||||
|
||||
Reference in New Issue
Block a user