Commit 2d23b863 by zhanghj

V1.1.0--提交判断是否绑定微信公众号和微信小程序,并免密代码 --zhanghj

git-svn-id: svn://192.168.3.233/frms/code/car-service/trunk/car-service@20299 aeb6102e-e053-11e5-9fec-f9ebc45caf4b
1 parent d9f14729
...@@ -6,7 +6,6 @@ import java.util.Map; ...@@ -6,7 +6,6 @@ import java.util.Map;
import javax.servlet.Filter; import javax.servlet.Filter;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SessionsSecurityManager; import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.realm.Realm; import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager; import org.apache.shiro.session.mgt.SessionManager;
...@@ -23,6 +22,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -23,6 +22,7 @@ import org.springframework.context.annotation.Configuration;
import com.glsx.usercenter.constants.Constants; import com.glsx.usercenter.constants.Constants;
import com.glsx.usercenter.filter.AuthFilter; import com.glsx.usercenter.filter.AuthFilter;
import com.glsx.usercenter.shiro.MyRetryLimitCredentialsMatcher;
import com.glsx.usercenter.shiro.UserRealm; import com.glsx.usercenter.shiro.UserRealm;
/** /**
...@@ -152,12 +152,22 @@ public class ShiroConfig { ...@@ -152,12 +152,22 @@ public class ShiroConfig {
* *
* @return * @return
*/ */
@Bean /* @Bean 注释掉自带的凭证匹配器
public HashedCredentialsMatcher hashedCredentialsMatcher() { public HashedCredentialsMatcher hashedCredentialsMatcher() {
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法;
hashedCredentialsMatcher.setHashIterations(1);//散列的次数,比如散列两次,相当于 md5(md5("")); hashedCredentialsMatcher.setHashIterations(1);//散列的次数,比如散列两次,相当于 md5(md5(""));
return hashedCredentialsMatcher; return hashedCredentialsMatcher;
}*/
@Bean(name = "myRetryLimitCredentialsMatcher")
public MyRetryLimitCredentialsMatcher hashedCredentialsMatcher() {
MyRetryLimitCredentialsMatcher hashedCredentialsMatcher = new MyRetryLimitCredentialsMatcher();
// 采用MD5方式加密
hashedCredentialsMatcher.setHashAlgorithmName("MD5");//散列算法:这里使用MD5算法;
// 设置加密次数
hashedCredentialsMatcher.setHashIterations(1);//散列的次数,比如散列两次,相当于 md5(md5(""));
return hashedCredentialsMatcher;
} }
/** /**
......
package com.glsx.usercenter.constants;
/**
*
* @Title LoginType.java
* @Package com.glsx.usercenter.constants
* @Description TODO 登录类型
* @author zhanghj
* @date 2019年4月25日 上午10:15:41
* @version 1.0.0
*/
public enum LoginType {
PASSWORD("password"), // 密码登录
NOPASSWD("nopassword"); // 免密登录
private String code;// 状态值
private LoginType(String code) {
this.code = code;
}
public String getCode () {
return code;
}
}
...@@ -42,6 +42,7 @@ import com.glsx.common.utils.DateUtil; ...@@ -42,6 +42,7 @@ import com.glsx.common.utils.DateUtil;
import com.glsx.common.utils.Page; import com.glsx.common.utils.Page;
import com.glsx.common.utils.StringUtils; import com.glsx.common.utils.StringUtils;
import com.glsx.common.utils.wehcat.WechatMiniUtils; import com.glsx.common.utils.wehcat.WechatMiniUtils;
import com.glsx.usercenter.constants.LoginType;
import com.glsx.usercenter.constants.ResultCodeEnum; import com.glsx.usercenter.constants.ResultCodeEnum;
import com.glsx.usercenter.dto.MerchantInfo; import com.glsx.usercenter.dto.MerchantInfo;
import com.glsx.usercenter.mapper.SysUserImageMapper; import com.glsx.usercenter.mapper.SysUserImageMapper;
...@@ -58,6 +59,7 @@ import com.glsx.usercenter.pojo.vo.UserQueryVo; ...@@ -58,6 +59,7 @@ import com.glsx.usercenter.pojo.vo.UserQueryVo;
import com.glsx.usercenter.service.IAreaService; import com.glsx.usercenter.service.IAreaService;
import com.glsx.usercenter.service.ISysOrganizationService; import com.glsx.usercenter.service.ISysOrganizationService;
import com.glsx.usercenter.service.SysUserService; import com.glsx.usercenter.service.SysUserService;
import com.glsx.usercenter.shiro.CustomToken;
import com.glsx.usercenter.utils.AccountValidatorUtil; import com.glsx.usercenter.utils.AccountValidatorUtil;
import com.glsx.usercenter.utils.QRCodeUtil; import com.glsx.usercenter.utils.QRCodeUtil;
import com.glsx.usercenter.utils.ShiroUtil; import com.glsx.usercenter.utils.ShiroUtil;
...@@ -846,7 +848,8 @@ public class UserController extends BaseController { ...@@ -846,7 +848,8 @@ public class UserController extends BaseController {
* shiro登录校验 * shiro登录校验
*/ */
Subject subject = SecurityUtils.getSubject(); Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password); CustomToken token = new CustomToken(username, password, LoginType.PASSWORD, false, null);
//UsernamePasswordToken token = new UsernamePasswordToken(username, password);
subject.login(token); subject.login(token);
session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERNAME_KEY, username); session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERNAME_KEY, username);
session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERID_KEY, user.getId()); session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERID_KEY, user.getId());
...@@ -1001,7 +1004,7 @@ public class UserController extends BaseController { ...@@ -1001,7 +1004,7 @@ public class UserController extends BaseController {
*/ */
@RequestMapping(value="/isBindWechat", method={RequestMethod.POST}) @RequestMapping(value="/isBindWechat", method={RequestMethod.POST})
public ResultObject isBindWechat(HttpSession session, Integer userType, String miniCode){ public ResultObject isBindWechat(HttpSession session, Integer userType, String miniCode){
logger.info("miniCode=====" + miniCode); logger.info("是否绑定微信接口miniCode=====" + miniCode);
ResultObject resultObject = new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg()); ResultObject resultObject = new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg());
boolean isPublicAuth = false; boolean isPublicAuth = false;
boolean isMiniAuth = false; boolean isMiniAuth = false;
...@@ -1024,7 +1027,7 @@ public class UserController extends BaseController { ...@@ -1024,7 +1027,7 @@ public class UserController extends BaseController {
} }
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
logger.error("获取微信小程序openid失败", e); logger.error("是否绑定微信获取微信小程序openid失败", e);
} }
if (!org.springframework.util.StringUtils.isEmpty(miniOpenId)) { if (!org.springframework.util.StringUtils.isEmpty(miniOpenId)) {
SysUser sysUser = new SysUser(); SysUser sysUser = new SysUser();
...@@ -1040,9 +1043,12 @@ public class UserController extends BaseController { ...@@ -1040,9 +1043,12 @@ public class UserController extends BaseController {
if (!org.springframework.util.StringUtils.isEmpty(publicOpenId)) { if (!org.springframework.util.StringUtils.isEmpty(publicOpenId)) {
isPublicAuth = true; isPublicAuth = true;
} }
/*
* shiro登录校验
*/
Subject subject = SecurityUtils.getSubject(); Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password); //UsernamePasswordToken token = new UsernamePasswordToken(username, password);
CustomToken token = new CustomToken(username, password, LoginType.NOPASSWD, false, null);
subject.login(token); subject.login(token);
session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERNAME_KEY, username); session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERNAME_KEY, username);
session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERID_KEY, user.getId()); session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERID_KEY, user.getId());
......
package com.glsx.usercenter.shiro;
import org.apache.shiro.authc.UsernamePasswordToken;
import com.glsx.usercenter.constants.LoginType;
/**
*
* @Title CustomToken.java
* @Package com.glsx.usercenter.shiro
* @Description TODO 免密验证
* @author zhanghj
* @date 2019年4月25日 上午10:18:41
* @version 1.0.0
*/
public class CustomToken extends UsernamePasswordToken{
private LoginType type;
public CustomToken() {
super();
}
public CustomToken(String username, String password, LoginType type, boolean rememberMe, String host) {
super(username, password, rememberMe, host);
this.type = type;
}
/**免密登录*/
public CustomToken(String username) {
super(username, "", false, null);
this.type = LoginType.NOPASSWD;
}
/**账号密码登录*/
public CustomToken(String username, String pwd) {
super(username, pwd, false, null);
this.type = LoginType.PASSWORD;
}
public LoginType getType() {
return type;
}
public void setType(LoginType type) {
this.type = type;
}
}
package com.glsx.usercenter.shiro;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.springframework.context.annotation.Configuration;
import com.glsx.usercenter.constants.LoginType;
/**
*
* @Title MyRetryLimitCredentialsMatcher.java
* @Package com.glsx.usercenter.shiro
* @Description TODO 密码凭证管理器
* @author zhanghj
* @date 2019年4月25日 上午10:43:04
* @version 1.0.0
*/
@Configuration
public class MyRetryLimitCredentialsMatcher extends HashedCredentialsMatcher {
@Override
public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) {
CustomToken tk = (CustomToken) authcToken;
if(tk.getType().equals(LoginType.NOPASSWD)){
return true;
}
boolean matches = super.doCredentialsMatch(authcToken, info);
return matches;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!