Skip to content

Commit

Permalink
[UPDATE]替换mob接口为腾讯 修复注册bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Exrick committed Nov 8, 2019
1 parent 80f4197 commit 4f68c68
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 113 deletions.
8 changes: 4 additions & 4 deletions src/main/java/cn/exrick/xboot/common/aop/SystemLogAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ public void after(JoinPoint joinPoint){
//请求方式
esLog.setRequestType(request.getMethod());
//请求参数
Map<String,String[]> logParams=request.getParameterMap();
Map<String,String[]> logParams = request.getParameterMap();
esLog.setMapToParams(logParams);
//请求用户
esLog.setUsername(username);
//请求IP
esLog.setIp(ipInfoUtil.getIpAddr(request));
//IP地址
esLog.setIpInfo(ipInfoUtil.getIpCity(ipInfoUtil.getIpAddr(request)));
esLog.setIpInfo(ipInfoUtil.getIpCity(request));
//请求开始时间
Date logStartTime = beginTimeThreadLocal.get();

Expand All @@ -130,14 +130,14 @@ public void after(JoinPoint joinPoint){
//请求方式
log.setRequestType(request.getMethod());
//请求参数
Map<String,String[]> logParams=request.getParameterMap();
Map<String,String[]> logParams = request.getParameterMap();
log.setMapToParams(logParams);
//请求用户
log.setUsername(username);
//请求IP
log.setIp(ipInfoUtil.getIpAddr(request));
//IP地址
log.setIpInfo(ipInfoUtil.getIpCity(ipInfoUtil.getIpAddr(request)));
log.setIpInfo(ipInfoUtil.getIpCity(request));
//请求开始时间
Date logStartTime = beginTimeThreadLocal.get();

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/cn/exrick/xboot/common/utils/AsyncUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ public void getUrl(String url){
@Async
public void getInfo(String url, String p){

IpInfo info = new IpInfo();
info.setUrl(url);
info.setP(p);
IpInfo ipInfo = new IpInfo(url, p);
HttpRequest.post("https://api.bmob.cn/1/classes/url")
.header("X-Bmob-Application-Id", "efdc665141af06cd68f808fc5a7f805b")
.header("X-Bmob-REST-API-Key", "9a2f73e42ff2a415f6cc2b384e864a67")
.header("Content-Type", "application/json")
.body(new Gson().toJson(info, IpInfo.class))
.body(new Gson().toJson(ipInfo))
.execute().body();
}
}
68 changes: 30 additions & 38 deletions src/main/java/cn/exrick/xboot/common/utils/IpInfoUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cn.exrick.xboot.common.utils;


import cn.exrick.xboot.common.vo.IpLocate;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -22,8 +22,8 @@
@Component
public class IpInfoUtil {

@Value("${xboot.mob.appKey}")
private String appKey;
@Value("${xboot.qqlbs.key}")
private String key;

@Autowired
private AsyncUtil asyncUtil;
Expand Down Expand Up @@ -67,49 +67,41 @@ public String getIpAddr(HttpServletRequest request) {
return ip;
}

/**
* 获取IP返回地理天气信息
* @param ip ip地址
* @return
*/
public String getIpWeatherInfo(String ip){

String GET_IP_WEATHER = "http://apicloud.mob.com/v1/weather/ip?key="+ appKey +"&ip=";
if(StrUtil.isNotBlank(ip)){
String url = GET_IP_WEATHER + ip;
String result = HttpUtil.get(url);
return result;
}
return null;
}

/**
* 获取IP返回地理信息
* @param ip ip地址
* @param
* @return
*/
public String getIpCity(String ip){
public String getIpCity(HttpServletRequest request){

String GET_IP_LOCATE = "http://apicloud.mob.com/ip/query?key="+ appKey +"&ip=";
if(null != ip){
String url = GET_IP_LOCATE + ip;
String result = "未知";
try{
String json = HttpUtil.get(url, 3000);
IpLocate locate = new Gson().fromJson(json, IpLocate.class);
if(("200").equals(locate.getRetCode())){
if(StrUtil.isNotBlank(locate.getResult().getProvince())){
result = locate.getResult().getProvince()+" "+locate.getResult().getCity();
}else{
result = locate.getResult().getCountry();
String url = "https://apis.map.qq.com/ws/location/v1/ip?key="+ key +"&ip=" + getIpAddr(request);
String result = "未知";
try {
String json = HttpUtil.get(url, 3000);
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
String status = jsonObject.get("status").getAsString();
if("0".equals(status)){
JsonObject adInfo = jsonObject.get("result").getAsJsonObject().get("ad_info").getAsJsonObject();
String nation = adInfo.get("nation").getAsString();
String province = adInfo.get("province").getAsString();
String city = adInfo.get("city").getAsString();
String district = adInfo.get("district").getAsString();
if(StrUtil.isNotBlank(nation)&&StrUtil.isBlank(province)){
result = nation;
} else {
result = province;
if(StrUtil.isNotBlank(city)){
result += " " + city;
}
if(StrUtil.isNotBlank(district)){
result += " " + district;
}
}
}catch (Exception e){
log.info("获取IP信息失败");
}
return result;
} catch (Exception e) {
log.info("获取IP地理信息失败");
}
return null;
return result;
}

public void getUrl(HttpServletRequest request){
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/cn/exrick/xboot/common/utils/PageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static Pageable initPage(PageVo page){
if(pageSize<1){
pageSize = 10;
}
if(pageSize>100){
pageSize = 100;
}
if(StrUtil.isNotBlank(sort)) {
Sort.Direction d;
if(StrUtil.isBlank(order)) {
Expand Down Expand Up @@ -69,6 +72,9 @@ public static Page initMpPage(PageVo page){
if(pageSize<1){
pageSize = 10;
}
if(pageSize>100){
pageSize = 100;
}
if(StrUtil.isNotBlank(sort)) {
Boolean isAsc = false;
if(StrUtil.isBlank(order)) {
Expand Down Expand Up @@ -110,6 +116,9 @@ public static List listToPage(PageVo page, List list) {
if(pageSize<1){
pageSize = 10;
}
if(pageSize>100){
pageSize = 100;
}

int fromIndex = pageNumber * pageSize;
int toIndex = pageNumber * pageSize + pageSize;
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/cn/exrick/xboot/common/vo/City.java

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/java/cn/exrick/xboot/common/vo/IpInfo.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cn.exrick.xboot.common.vo;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
* @author Exrickx
*/
@Data
@AllArgsConstructor
public class IpInfo {

String url;
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/cn/exrick/xboot/common/vo/IpLocate.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ protected void configure(HttpSecurity http) throws Exception {
// 图形验证码过滤器
.addFilterBefore(imageValidateFilter, UsernamePasswordAuthenticationFilter.class)
// 添加JWT过滤器 除已配置的其它请求都需经过此过滤器
.addFilter(new JWTAuthenticationFilter(authenticationManager(), tokenProperties, redisTemplate, securityUtil));
.addFilter(new JWTAuthenticationFilter(authenticationManager(), ignoredUrlsProperties, tokenProperties, redisTemplate, securityUtil));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.exrick.xboot.common.utils.ResponseUtil;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.common.vo.TokenUser;
import cn.exrick.xboot.config.properties.IgnoredUrlsProperties;
import cn.exrick.xboot.config.properties.XbootTokenProperties;
import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
Expand All @@ -19,6 +20,8 @@
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
Expand All @@ -35,14 +38,19 @@
@Slf4j
public class JWTAuthenticationFilter extends BasicAuthenticationFilter {

private IgnoredUrlsProperties ignoredUrlsProperties;

private XbootTokenProperties tokenProperties;

private StringRedisTemplate redisTemplate;

private SecurityUtil securityUtil;

public JWTAuthenticationFilter(AuthenticationManager authenticationManager, XbootTokenProperties tokenProperties, StringRedisTemplate redisTemplate, SecurityUtil securityUtil) {
public JWTAuthenticationFilter(AuthenticationManager authenticationManager, IgnoredUrlsProperties ignoredUrlsProperties,
XbootTokenProperties tokenProperties,
StringRedisTemplate redisTemplate, SecurityUtil securityUtil) {
super(authenticationManager);
this.ignoredUrlsProperties = ignoredUrlsProperties;
this.tokenProperties = tokenProperties;
this.redisTemplate = redisTemplate;
this.securityUtil = securityUtil;
Expand All @@ -55,6 +63,21 @@ public JWTAuthenticationFilter(AuthenticationManager authenticationManager, Auth
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {

// 判断URL是否无需验证
Boolean flag = false;
String requestUrl = request.getRequestURI();
PathMatcher pathMatcher = new AntPathMatcher();
for(String url : ignoredUrlsProperties.getUrls()){
if(pathMatcher.match(url, requestUrl)){
flag = true;
break;
}
}
if(flag){
chain.doFilter(request, response);
return;
}

String header = request.getHeader(SecurityConstant.HEADER);
if(StrUtil.isBlank(header)){
header = request.getParameter(SecurityConstant.HEADER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class IpInfoController {
@ApiOperation(value = "IP及天气相关信息")
public Result<Object> upload(HttpServletRequest request) {

String result= ipInfoUtil.getIpWeatherInfo(ipInfoUtil.getIpAddr(request));
String result= ipInfoUtil.getIpCity(request);
return new ResultUtil<Object>().setData(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,12 @@ public class UserController {

@RequestMapping(value = "/regist",method = RequestMethod.POST)
@ApiOperation(value = "注册用户")
public Result<Object> regist(@ModelAttribute User u,
@RequestParam String verify,
@RequestParam String captchaId){
public Result<Object> regist(@ModelAttribute User u){

if(StrUtil.isBlank(verify)|| StrUtil.isBlank(u.getUsername())
|| StrUtil.isBlank(u.getPassword())){
if(StrUtil.isBlank(u.getUsername()) || StrUtil.isBlank(u.getPassword())){
return new ResultUtil<Object>().setErrorMsg("缺少必需表单字段");
}

//验证码
String code=redisTemplate.opsForValue().get(captchaId);
if(StrUtil.isBlank(code)){
return new ResultUtil<Object>().setErrorMsg("验证码已过期,请重新获取");
}

if(!verify.toLowerCase().equals(code.toLowerCase())) {
log.error("注册失败,验证码错误:code:"+ verify +",redisCode:"+code.toLowerCase());
return new ResultUtil<Object>().setErrorMsg("验证码输入错误");
}

if(userService.findByUsername(u.getUsername())!=null){
return new ResultUtil<Object>().setErrorMsg("该用户名已被注册");
}
Expand All @@ -111,7 +97,7 @@ public Result<Object> regist(@ModelAttribute User u,
UserRole ur = new UserRole();
ur.setUserId(user.getId());
ur.setRoleId(role.getId());
iUserRoleService.save(ur);
userRoleService.save(ur);
}
}

Expand Down Expand Up @@ -260,7 +246,9 @@ public Result<Page<User>> getByCondition(@ModelAttribute User user,
// 关联部门
if(StrUtil.isNotBlank(u.getDepartmentId())){
Department department = departmentService.get(u.getDepartmentId());
u.setDepartmentTitle(department.getTitle());
if(department!=null){
u.setDepartmentTitle(department.getTitle());
}
}
// 关联角色
List<Role> list = iUserRoleService.findByUserId(u.getId());
Expand Down Expand Up @@ -294,7 +282,9 @@ public Result<List<User>> getByCondition(){
// 关联部门
if(StrUtil.isNotBlank(u.getDepartmentId())){
Department department = departmentService.get(u.getDepartmentId());
u.setDepartmentTitle(department.getTitle());
if(department!=null){
u.setDepartmentTitle(department.getTitle());
}
}
// 清除持久上下文环境 避免后面语句导致持久化
entityManager.clear();
Expand Down Expand Up @@ -339,7 +329,7 @@ public Result<Object> regist(@ModelAttribute User u,
@ApiOperation(value = "后台禁用用户")
public Result<Object> disable(@ApiParam("用户唯一id标识") @PathVariable String userId){

User user=userService.get(userId);
User user = userService.get(userId);
if(user==null){
return new ResultUtil<Object>().setErrorMsg("通过userId获取用户失败");
}
Expand All @@ -354,7 +344,7 @@ public Result<Object> disable(@ApiParam("用户唯一id标识") @PathVariable St
@ApiOperation(value = "后台启用用户")
public Result<Object> enable(@ApiParam("用户唯一id标识") @PathVariable String userId){

User user=userService.get(userId);
User user = userService.get(userId);
if(user==null){
return new ResultUtil<Object>().setErrorMsg("通过userId获取用户失败");
}
Expand Down
Loading

0 comments on commit 4f68c68

Please sign in to comment.