| @@ -98,12 +98,27 @@ public class ShiroConfig { | |||
| filterChainDefinitionMap.put("/logout", "anon"); | |||
| //filterChainDefinitionMap.put("/ue/**", "anon"); | |||
| //filterChainDefinitionMap.put("/config.json", "anon"); | |||
| // login | |||
| filterChainDefinitionMap.put("/doLogin/**", "anon"); | |||
| filterChainDefinitionMap.put("/bHidLogin/**", "anon"); | |||
| filterChainDefinitionMap.put("/wechat/callback", "anon"); // 微信第三方登录 | |||
| // callback | |||
| filterChainDefinitionMap.put("/wxPay/notify/**", "anon"); // 支付回调 | |||
| filterChainDefinitionMap.put("/wxPayBill/notify/**", "anon"); | |||
| filterChainDefinitionMap.put("/wxMsgCallback/**", "anon"); | |||
| filterChainDefinitionMap.put("/user/sendvalidationcode", "anon"); | |||
| filterChainDefinitionMap.put("/user/updatepwd", "anon"); | |||
| filterChainDefinitionMap.put("/carCallback/**", "anon"); | |||
| filterChainDefinitionMap.put("/wxMallApply/add", "anon"); | |||
| filterChainDefinitionMap.put("/wxMallApply/sendvalidationcode", "anon"); | |||
| // static files | |||
| filterChainDefinitionMap.put("/css/**", "anon"); | |||
| filterChainDefinitionMap.put("/js/**", "anon"); | |||
| filterChainDefinitionMap.put("/img/**", "anon"); | |||
| filterChainDefinitionMap.put("/font-awesome/**", "anon"); | |||
| // 验证码 | |||
| filterChainDefinitionMap.put("/captcha.jpg", "anon"); | |||
| //<!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; | |||
| //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> | |||
| //自定义加载权限资源关系 | |||
| @@ -116,20 +131,12 @@ public class ShiroConfig { | |||
| // filterChainDefinitionMap.put(resources.getUrl(),permission); | |||
| // } | |||
| // } | |||
| // swagger-ui | |||
| filterChainDefinitionMap.put("/swagger-ui.html", "anon"); | |||
| filterChainDefinitionMap.put("/wxPay/notify/**", "anon"); | |||
| filterChainDefinitionMap.put("/wxOpen/**", "anon"); | |||
| filterChainDefinitionMap.put("/wxPayBill/notify/**", "anon"); | |||
| filterChainDefinitionMap.put("/v2/**", "anon"); | |||
| filterChainDefinitionMap.put("/swagger-resources/**", "anon"); | |||
| filterChainDefinitionMap.put("/webjars/**", "anon"); | |||
| filterChainDefinitionMap.put("/wxMsgCallback/**", "anon"); | |||
| filterChainDefinitionMap.put("/user/sendvalidationcode", "anon"); | |||
| filterChainDefinitionMap.put("/user/updatepwd", "anon"); | |||
| filterChainDefinitionMap.put("/carCallback/**", "anon"); | |||
| filterChainDefinitionMap.put("/wxMallApply/add", "anon"); | |||
| filterChainDefinitionMap.put("/wxMallApply/sendvalidationcode", "anon"); | |||
| filterChainDefinitionMap.put("/captcha.jpg", "anon"); | |||
| filterChainDefinitionMap.put("/version", "anon"); | |||
| // filterChainDefinitionMap.put("/role/**", "corsFilter,token"); | |||
| filterChainDefinitionMap.put("/**", "corsFilter,token,authc"); | |||
| @@ -0,0 +1,33 @@ | |||
| package com.iformall.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; | |||
| @Component | |||
| public class WechatMpConfig { | |||
| @Autowired | |||
| private WechatWebProperties wechatWebProperties; | |||
| @Bean | |||
| public WxMpService wxMpService() { | |||
| //创建WxMpService实例并设置appid和sectret | |||
| WxMpService wxMpService = new WxMpServiceImpl(); | |||
| //这里的设置方式是跟着这个sdk的文档写的 | |||
| wxMpService.setWxMpConfigStorage(wxConfigProvider()); | |||
| return wxMpService; | |||
| } | |||
| @Bean | |||
| public WxMpConfigStorage wxConfigProvider(){ | |||
| WxMpInMemoryConfigStorage wxConfigProvider = new WxMpInMemoryConfigStorage(); | |||
| wxConfigProvider.setAppId(wechatWebProperties.getAppId()); | |||
| wxConfigProvider.setSecret(wechatWebProperties.getSecret()); | |||
| return wxConfigProvider; | |||
| } | |||
| } | |||
| @@ -0,0 +1,59 @@ | |||
| package com.iformall.config; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import org.springframework.boot.context.properties.ConfigurationProperties; | |||
| import org.springframework.stereotype.Component; | |||
| /** | |||
| * Stormeye | |||
| */ | |||
| @Component | |||
| @ConfigurationProperties(prefix = "wechat.web") | |||
| public class WechatWebProperties { | |||
| /** | |||
| * 设置微信第三方平台-微信登录的web应用appid | |||
| */ | |||
| private String appId; | |||
| /** | |||
| * 设置微信第三方平台-微信登录的web应用app secret | |||
| */ | |||
| private String secret; | |||
| /** | |||
| * 网页URL | |||
| * @return | |||
| */ | |||
| private String url; | |||
| public String getAppId() { | |||
| return appId; | |||
| } | |||
| public void setAppId(String appId) { | |||
| this.appId = appId; | |||
| } | |||
| public String getSecret() { | |||
| return secret; | |||
| } | |||
| public void setSecret(String secret) { | |||
| this.secret = secret; | |||
| } | |||
| public String getUrl() { | |||
| return url; | |||
| } | |||
| public void setUrl(String url) { | |||
| this.url = url; | |||
| } | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, | |||
| ToStringStyle.MULTI_LINE_STYLE); | |||
| } | |||
| } | |||
| @@ -115,8 +115,10 @@ public class HomeController extends BaseController { | |||
| // } | |||
| MallUserInfo info = (MallUserInfo) SecurityUtils.getSubject().getSession().getAttribute(UserSession.userInfo); | |||
| info.setPassword("保密"); | |||
| info.setBopenId("保密"); | |||
| info.setWebOpenId("保密"); | |||
| if(StringUtils.isNotBlank(info.getBopenId())) | |||
| info.setBopenId("保密"); | |||
| if(StringUtils.isNotBlank(info.getWebOpenId())) | |||
| info.setWebOpenId("保密"); | |||
| // System.out.println("id:"+ SecurityUtils.getSubject().getSession().getId()); | |||
| String menus = mallUserRoleService.getPermissionsByUser(info); | |||
| if(menus != null) { | |||
| @@ -183,8 +185,10 @@ public class HomeController extends BaseController { | |||
| // } | |||
| MallUserInfo info = (MallUserInfo) SecurityUtils.getSubject().getSession().getAttribute(UserSession.userInfo); | |||
| info.setPassword("保密"); | |||
| info.setBopenId("保密"); | |||
| info.setWebOpenId("保密"); | |||
| if(StringUtils.isNotBlank(info.getBopenId())) | |||
| info.setBopenId("保密"); | |||
| if(StringUtils.isNotBlank(info.getWebOpenId())) | |||
| info.setWebOpenId("保密"); | |||
| // System.out.println("id:"+ SecurityUtils.getSubject().getSession().getId()); | |||
| String menus = mallUserRoleService.getPermissionsByUser(info); | |||
| if(menus != null) { | |||
| @@ -0,0 +1,94 @@ | |||
| package com.iformall.controller; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.domain.po.MallUserInfo; | |||
| import com.iformall.service.MallUserInfoService; | |||
| import com.iformall.service.MallUserRoleService; | |||
| import com.iformall.shiro.UserSession; | |||
| import com.iformall.shiro.UseriFormallToken; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| import me.chanjar.weixin.common.error.WxErrorException; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.shiro.SecurityUtils; | |||
| import org.apache.shiro.subject.Subject; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Controller; | |||
| import org.springframework.web.bind.annotation.*; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| @Controller | |||
| @RequestMapping("/wechat") | |||
| @Slf4j | |||
| public class WechatLoginController extends BaseController { | |||
| @Autowired | |||
| private WxMpService wxMpService; | |||
| @Autowired | |||
| private MallUserInfoService mallUserInfoService; | |||
| @Autowired | |||
| private MallUserRoleService mallUserRoleService; | |||
| @ApiOperation(value = "微信网页登录回调", notes = "请配置此callback到网页redirect_uri") | |||
| @GetMapping("callback") | |||
| public ResultData getAccessToken(String code) { | |||
| ResultData r = new ResultData(); | |||
| try { | |||
| WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code); | |||
| // check user openid 是否是已授权用户 | |||
| MallUserInfo user = mallUserInfoService.getByWebOpenId(accessToken.getOpenId()); | |||
| if(user != null) { | |||
| try { | |||
| Subject subject = SecurityUtils.getSubject(); | |||
| UseriFormallToken token = new UseriFormallToken(user.getUsername()); | |||
| subject.login(token); | |||
| MallUserInfo info = (MallUserInfo) SecurityUtils.getSubject().getSession().getAttribute(UserSession.userInfo); | |||
| info.setPassword("保密"); | |||
| info.setBopenId("保密"); | |||
| info.setWebOpenId("保密"); | |||
| String menus = mallUserRoleService.getPermissionsByUser(info); | |||
| if(menus != null) { | |||
| info.setMenus(menus); | |||
| } | |||
| r.data = info; | |||
| } catch (Exception e) { | |||
| return new ResultData(ResultData.ERROR, "用户名或者密码错误"); | |||
| } | |||
| } else { | |||
| Map<String, String> map = new HashMap<String, String>(); | |||
| map.put("accessToken", accessToken.getAccessToken()); | |||
| map.put("openId", accessToken.getOpenId()); | |||
| map.put("unionId", accessToken.getUnionId()); | |||
| r.data = map; | |||
| } | |||
| return r; | |||
| } catch (WxErrorException e) { | |||
| log.error(e.getMessage()); | |||
| return new ResultData(); | |||
| } | |||
| } | |||
| @ApiOperation(value = "微信第三方登录设置", notes = "请配置此callback到网页redirect_uri") | |||
| @GetMapping("bindWebOpenId") | |||
| public ResultData userBindWebOpenId(String code) { | |||
| MallUserInfo user = getUser(); | |||
| ResultData r = new ResultData(); | |||
| try { | |||
| WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code); | |||
| MallUserInfo updateUser = new MallUserInfo(); | |||
| updateUser.setId(user.getId()); | |||
| updateUser.setWebOpenId(accessToken.getOpenId()); | |||
| mallUserInfoService.saveOrUpdate(updateUser); | |||
| return r; | |||
| } catch (WxErrorException e) { | |||
| log.error(e.getMessage()); | |||
| return new ResultData(); | |||
| } | |||
| } | |||
| } | |||
| @@ -57,6 +57,12 @@ aws: | |||
| access: ENC(3gx5ghDFBqGrEhO3Wf8aYmXsnwHO7Cj3HNKJGOeUj0o=) | |||
| secret: ENC(HVKIJwCJKVXLlUpGlQPwNqJOlnpxn4xYuy91SH0seTSm2uAttIQHvA49fXWWax90v5wloIk0QuU=) | |||
| wechat: | |||
| web: | |||
| appId: "wx897e4673286c915d" | |||
| secret: "cdfdfda65c45689beb6766c4c427eed2" | |||
| url: "https://mall.youlane.cn" | |||
| jasypt: | |||
| encryptor: | |||
| password: oRqdnDbK5pj3eMmB | |||