This commit is contained in:
李寻欢 2017-12-07 10:32:05 +08:00
commit bab5679b2c
6 changed files with 116 additions and 42 deletions

View File

@ -0,0 +1,55 @@
package com.github.pig.admin.common.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author lengleng
* @date 2017/12/6
* 七牛参数
*/
@Configuration
@ConfigurationProperties(prefix = "qiniu")
public class QiniuPropertiesConfig {
@Value("${qiniu.accessKey}")
private String accessKey;
@Value("${qiniu.secretKey}")
private String secretKey;
@Value("${qiniu.bucket}")
private String bucket;
@Value("${qiniu.host}")
private String qiniuHost;
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getBucket() {
return bucket;
}
public void setBucket(String bucket) {
this.bucket = bucket;
}
public String getQiniuHost() {
return qiniuHost;
}
public void setQiniuHost(String qiniuHost) {
this.qiniuHost = qiniuHost;
}
}

View File

@ -1,4 +1,4 @@
package com.github.pig.gateway.config; package com.github.pig.admin.common.config;
import com.github.pig.common.constant.CommonConstant; import com.github.pig.common.constant.CommonConstant;
import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.Queue;
@ -12,8 +12,12 @@ import org.springframework.context.annotation.Configuration;
*/ */
@Configuration @Configuration
public class RabbitConfig { public class RabbitConfig {
/**
* 初始化 log队列
* @return
*/
@Bean @Bean
public Queue helloQueue() { public Queue initLogQueue() {
return new Queue(CommonConstant.LOG_QUEUE); return new Queue(CommonConstant.LOG_QUEUE);
} }
} }

View File

@ -2,6 +2,7 @@ package com.github.pig.admin.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.common.config.QiniuPropertiesConfig;
import com.github.pig.admin.dto.UserDto; import com.github.pig.admin.dto.UserDto;
import com.github.pig.admin.dto.UserInfo; import com.github.pig.admin.dto.UserInfo;
import com.github.pig.admin.entity.SysUser; import com.github.pig.admin.entity.SysUser;
@ -13,29 +14,20 @@ import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.UserUtils; import com.github.pig.common.util.UserUtils;
import com.github.pig.common.vo.UserVo; import com.github.pig.common.vo.UserVo;
import com.github.pig.common.web.BaseController; import com.github.pig.common.web.BaseController;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone; import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration; import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager; import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth; import com.qiniu.util.Auth;
import com.xiaoleilu.hutool.io.FileUtil; import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.util.RandomUtil; import com.xiaoleilu.hutool.util.RandomUtil;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -54,6 +46,8 @@ public class UserController extends BaseController {
private SysUserRoleService sysUserRoleService; private SysUserRoleService sysUserRoleService;
@Autowired @Autowired
private SysMenuService sysMenuService; private SysMenuService sysMenuService;
@Autowired
private QiniuPropertiesConfig qiniuPropertiesConfig;
/** /**
@ -176,35 +170,19 @@ public class UserController extends BaseController {
@PostMapping("/upload") @PostMapping("/upload")
public Map<String, String> upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) { public Map<String, String> upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
String fileExt = FileUtil.extName(file.getOriginalFilename()); String fileExt = FileUtil.extName(file.getOriginalFilename());
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0()); Configuration cfg = new Configuration(Zone.zone0());
UploadManager uploadManager = new UploadManager(cfg); UploadManager uploadManager = new UploadManager(cfg);
String accessKey = "hM2cBDEM0FTYzpXbigRW90kV12NhhzhFM3jCzurJ";
String secretKey = "g0HJr2Ltrs0k6tJDY6pDI2aVMUCPSWZDTROLcFMs";
String bucket = "pigcloud";
String key = RandomUtil.randomUUID() + "." + fileExt; String key = RandomUtil.randomUUID() + "." + fileExt;
Auth auth = Auth.create(accessKey, secretKey); Auth auth = Auth.create(qiniuPropertiesConfig.getAccessKey(), qiniuPropertiesConfig.getSecretKey());
String upToken = auth.uploadToken(bucket); String upToken = auth.uploadToken(qiniuPropertiesConfig.getBucket());
try { try {
Response response = uploadManager.put(file.getInputStream(), key, upToken, null, null); uploadManager.put(file.getInputStream(), key, upToken, null, null);
//解析上传成功的结果 } catch (Exception e) {
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); logger.error("文件上传异常", e);
System.out.println(putRet.key); throw new RuntimeException(e);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
} catch (IOException e) {
e.printStackTrace();
} }
Map<String, String> resultMap = new HashMap<>(1); Map<String, String> resultMap = new HashMap<>(1);
resultMap.put("filename", "http://p0hpm86wj.bkt.clouddn.com/" + key); resultMap.put("filename", qiniuPropertiesConfig.getQiniuHost() + key);
return resultMap; return resultMap;
} }

View File

@ -19,7 +19,7 @@ import java.util.Base64;
*/ */
public class UserUtils { public class UserUtils {
private static Logger logger = LoggerFactory.getLogger(UserUtils.class); private static Logger logger = LoggerFactory.getLogger(UserUtils.class);
private static final ThreadLocal<String> tlUser = new ThreadLocal<String>(); private static final ThreadLocal<String> TL_User = new ThreadLocal<>();
private static final String KEY_USER = "user"; private static final String KEY_USER = "user";
@ -77,7 +77,7 @@ public class UserUtils {
* @param username * @param username
*/ */
public static void setUser(String username) { public static void setUser(String username) {
tlUser.set(username); TL_User.set(username);
MDC.put(KEY_USER, username); MDC.put(KEY_USER, username);
} }
@ -88,11 +88,11 @@ public class UserUtils {
* @return * @return
*/ */
public static String getUserName() { public static String getUserName() {
return tlUser.get(); return TL_User.get();
} }
public static void clearAllUserInfo() { public static void clearAllUserInfo() {
tlUser.remove(); TL_User.remove();
MDC.remove(KEY_USER); MDC.remove(KEY_USER);
} }
} }

View File

@ -0,0 +1,27 @@
package com.github.pig.gateway.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author lengleng
* @date 2017/12/6
*/
@Configuration
@ConfigurationProperties(prefix = "filter.urls")
public class FilterUrlsPropertiesConifg {
private List<String> anon = new ArrayList<>();
public List<String> getAnon() {
return anon;
}
public void setAnon(List<String> anon) {
this.anon = anon;
}
}

View File

@ -1,15 +1,20 @@
package com.github.pig.gateway.config; package com.github.pig.gateway.config;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; 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.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler; import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;
import java.util.List;
import java.util.Set;
/** /**
* @author lengleng * @author lengleng
* @date 2017/10/27 * @date 2017/10/27
@ -17,15 +22,20 @@ import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurity
@Configuration @Configuration
@EnableResourceServer @EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Autowired
private FilterUrlsPropertiesConifg filterUrlsPropertiesConifg;
@Autowired @Autowired
private OAuth2WebSecurityExpressionHandler expressionHandler; private OAuth2WebSecurityExpressionHandler expressionHandler;
@Override @Override
public void configure(HttpSecurity http) throws Exception { public void configure(HttpSecurity http) throws Exception {
http ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = http
.authorizeRequests() .authorizeRequests();
.antMatchers("/auth/**").permitAll() for (String url : filterUrlsPropertiesConifg.getAnon()) {
.anyRequest().access("@permissionService.hasPermission(request,authentication)"); registry.antMatchers(url).permitAll();
}
registry.anyRequest()
.access("@permissionService.hasPermission(request,authentication)");
} }
@Override @Override