forked from sqmax/springboot-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sqmax
committed
Apr 10, 2018
1 parent
99a30f7
commit 17e59e1
Showing
34 changed files
with
700 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,56 @@ | ||
package com.imooc.aspect; | ||
|
||
import com.imooc.constant.CookieConstant; | ||
import com.imooc.constant.RedisConstant; | ||
import com.imooc.exception.SellerAuthorizeException; | ||
import com.imooc.utils.CookieUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import javax.servlet.http.Cookie; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* Created by SqMax on 2018/4/2. | ||
*/ | ||
@Aspect | ||
@Component | ||
@Slf4j | ||
public class SellerAuthorizeAspect { | ||
|
||
@Autowired | ||
private StringRedisTemplate redisTemplate; | ||
|
||
// @Pointcut("execution(public * com.imooc.controller.Seller*.*(..))"+ | ||
// "&& !execution(public * com.imooc.controller.SellerUserController.*(..))") | ||
// public void verify(){} | ||
// | ||
// @Before("verify()") | ||
// public void doVerify(){ | ||
// ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
// HttpServletRequest request=attributes.getRequest(); | ||
// | ||
// //查询cookie | ||
// Cookie cookie= CookieUtil.get(request,CookieConstant.TOKEN); | ||
// if (cookie==null){ | ||
// log.warn("【登录校验】Cookie中查不到token"); | ||
// throw new SellerAuthorizeException(); | ||
// } | ||
// | ||
// //去redis查询 | ||
// String tokenValue=redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue())); | ||
// if (StringUtils.isEmpty(tokenValue)){ | ||
// log.warn("【登录校验】 Redis中查不到token"); | ||
// throw new SellerAuthorizeException(); | ||
// } | ||
// } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,30 @@ | ||
package com.imooc.config; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Created by SqMax on 2018/4/1. | ||
*/ | ||
public class ProjectUrl { | ||
@Data | ||
@ConfigurationProperties(prefix = "projectUrl") | ||
@Component | ||
public class ProjectUrlConfig { | ||
|
||
/** | ||
* 微信公众平台授权url | ||
*/ | ||
public String wechatMpAuthorize; | ||
|
||
/** | ||
* 微信开放平台授权url | ||
*/ | ||
public String wechatOpenAuthorize; | ||
|
||
/** | ||
* 点餐系统 | ||
*/ | ||
public String sell; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
package com.imooc.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.server.standard.ServerEndpointExporter; | ||
|
||
/** | ||
* Created by SqMax on 2018/4/2. | ||
*/ | ||
@Component | ||
public class WebSocketConfig { | ||
|
||
// @Bean | ||
// public ServerEndpointExporter serverEndpointExporter(){ | ||
// return new ServerEndpointExporter(); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,33 @@ | ||
package com.imooc.config; | ||
|
||
import me.chanjar.weixin.mp.api.WxMpConfigStorage; | ||
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; | ||
import me.chanjar.weixin.mp.api.WxMpService; | ||
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Created by SqMax on 2018/4/1. | ||
*/ | ||
@Component | ||
public class WechatOpenConfig { | ||
|
||
@Autowired | ||
private WechatAccountConfig accountConfig; | ||
|
||
@Bean | ||
public WxMpService wxOpenService(){ | ||
WxMpService wxOpenService=new WxMpServiceImpl(); | ||
wxOpenService.setWxMpConfigStorage(wxOpenConfigStorage()); | ||
return wxOpenService; | ||
} | ||
@Bean | ||
public WxMpConfigStorage wxOpenConfigStorage(){ | ||
WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage=new WxMpInMemoryConfigStorage(); | ||
wxMpInMemoryConfigStorage.setAppId(accountConfig.getOpenAppId()); | ||
wxMpInMemoryConfigStorage.setSecret(accountConfig.getOpenAppSecret()); | ||
return wxMpInMemoryConfigStorage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,8 @@ | |
* Created by SqMax on 2018/4/1. | ||
*/ | ||
public interface CookieConstant { | ||
|
||
String TOKEN="token"; | ||
|
||
Integer EXPIRE=7200; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package com.imooc.constant; | ||
|
||
/** | ||
* redis常量 | ||
* Created by SqMax on 2018/4/1. | ||
*/ | ||
public class RedisConstant { | ||
public interface RedisConstant { | ||
|
||
String TOKEN_PREFIX="token_%s"; | ||
|
||
Integer EXPIRE=7200;//2小时 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/imooc/controller/SellerUserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,82 @@ | ||
package com.imooc.controller; | ||
|
||
import com.imooc.config.ProjectUrlConfig; | ||
import com.imooc.constant.CookieConstant; | ||
import com.imooc.constant.RedisConstant; | ||
import com.imooc.dataobject.SellerInfo; | ||
import com.imooc.enums.ResultEnum; | ||
import com.imooc.service.SellerService; | ||
import com.imooc.utils.CookieUtil; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.stereotype.Controller; | ||
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.servlet.ModelAndView; | ||
|
||
import javax.servlet.http.Cookie; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Created by SqMax on 2018/4/1. | ||
*/ | ||
@Controller | ||
@RequestMapping("/seller") | ||
public class SellerUserController { | ||
|
||
@Autowired | ||
private SellerService sellerService; | ||
|
||
@Autowired | ||
private StringRedisTemplate redisTemplate; | ||
|
||
@Autowired | ||
private ProjectUrlConfig projectUrlConfig; | ||
|
||
@GetMapping("/login") | ||
public ModelAndView login(@RequestParam("openid") String openid, | ||
HttpServletResponse response, | ||
Map<String,Object> map){ | ||
|
||
//1.openid去和数据库里的数据匹配 | ||
SellerInfo sellerInfo=sellerService.findSellerInfoByOpenid(openid); | ||
if(sellerInfo==null){ | ||
map.put("msg", ResultEnum.LOGIN_FAIL.getMessage()); | ||
map.put("url","/sell/seller/order/list"); | ||
return new ModelAndView("common/error"); | ||
} | ||
//2.设置token至redis | ||
String token= UUID.randomUUID().toString(); | ||
Integer expire= RedisConstant.EXPIRE; | ||
redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),openid,expire, TimeUnit.SECONDS); | ||
|
||
//3.设置token至cookie | ||
CookieUtil.set(response, CookieConstant.TOKEN,token,expire); | ||
|
||
return new ModelAndView("redirect:"+projectUrlConfig.getSell()+"/sell/seller/order/list"); | ||
} | ||
|
||
@GetMapping("/logout") | ||
public ModelAndView logout(HttpServletRequest request, | ||
HttpServletResponse response, | ||
Map<String,Object> map){ | ||
|
||
//1.从cookie里查询 | ||
Cookie cookie=CookieUtil.get(request,CookieConstant.TOKEN); | ||
if(cookie!=null){ | ||
//2.清除redis | ||
redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue())); | ||
//3.清除cookie | ||
CookieUtil.set(response,CookieConstant.TOKEN,null,0); | ||
} | ||
map.put("msg",ResultEnum.LOGOUT_SUCCESS.getMessage()); | ||
map.put("url","/sell/seller/order/list"); | ||
return new ModelAndView("common/success",map); | ||
|
||
} | ||
} |
Oops, something went wrong.