| @@ -1,8 +1,8 @@ | |||||
| package com.iformall.config; | package com.iformall.config; | ||||
| import com.iformall.service.MallPermissionService; | import com.iformall.service.MallPermissionService; | ||||
| import com.iformall.shiro.MyRetryLimitCredentialsMatcher; | |||||
| import com.iformall.shiro.MyShiroRealm; | import com.iformall.shiro.MyShiroRealm; | ||||
| import org.apache.shiro.authc.credential.HashedCredentialsMatcher; | |||||
| import org.apache.shiro.mgt.SecurityManager; | import org.apache.shiro.mgt.SecurityManager; | ||||
| import org.apache.shiro.spring.LifecycleBeanPostProcessor; | import org.apache.shiro.spring.LifecycleBeanPostProcessor; | ||||
| import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; | import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; | ||||
| @@ -14,6 +14,7 @@ import org.crazycake.shiro.RedisCacheManager; | |||||
| import org.crazycake.shiro.RedisManager; | import org.crazycake.shiro.RedisManager; | ||||
| import org.crazycake.shiro.RedisSessionDAO; | import org.crazycake.shiro.RedisSessionDAO; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.beans.factory.annotation.Qualifier; | |||||
| import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.context.annotation.Bean; | import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | import org.springframework.context.annotation.Configuration; | ||||
| @@ -81,6 +82,7 @@ public class ShiroConfig { | |||||
| Map<String, Filter> filters = new LinkedHashMap<String, Filter>(); | Map<String, Filter> filters = new LinkedHashMap<String, Filter>(); | ||||
| filters.put("token", new ShiroLoginFilter()); | filters.put("token", new ShiroLoginFilter()); | ||||
| filters.put("corsFilter", new RestFilter()); | filters.put("corsFilter", new RestFilter()); | ||||
| //filters.put("authc", new MyFormAuthenticationFilter()); | |||||
| shiroFilterFactoryBean.setFilters(filters); | shiroFilterFactoryBean.setFilters(filters); | ||||
| // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 | // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 | ||||
| shiroFilterFactoryBean.setLoginUrl("/#/"); | shiroFilterFactoryBean.setLoginUrl("/#/"); | ||||
| @@ -95,6 +97,7 @@ public class ShiroConfig { | |||||
| //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 | //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 | ||||
| filterChainDefinitionMap.put("/logout", "anon"); | filterChainDefinitionMap.put("/logout", "anon"); | ||||
| filterChainDefinitionMap.put("/doLogin/**", "anon"); | filterChainDefinitionMap.put("/doLogin/**", "anon"); | ||||
| filterChainDefinitionMap.put("/bHidLogin/**", "anon"); | |||||
| filterChainDefinitionMap.put("/css/**", "anon"); | filterChainDefinitionMap.put("/css/**", "anon"); | ||||
| filterChainDefinitionMap.put("/js/**", "anon"); | filterChainDefinitionMap.put("/js/**", "anon"); | ||||
| filterChainDefinitionMap.put("/img/**", "anon"); | filterChainDefinitionMap.put("/img/**", "anon"); | ||||
| @@ -146,10 +149,10 @@ public class ShiroConfig { | |||||
| } | } | ||||
| @Bean | @Bean | ||||
| public SecurityManager securityManager() { | |||||
| public SecurityManager securityManager(@Qualifier("myRetryLimitCredentialsMatcher") MyRetryLimitCredentialsMatcher matcher) { | |||||
| DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); | DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); | ||||
| //设置realm. | //设置realm. | ||||
| securityManager.setRealm(myShiroRealm()); | |||||
| securityManager.setRealm(myShiroRealm(matcher)); | |||||
| // 自定义缓存实现 使用redis | // 自定义缓存实现 使用redis | ||||
| //securityManager.setCacheManager(cacheManager()); | //securityManager.setCacheManager(cacheManager()); | ||||
| // 自定义session管理 使用redis | // 自定义session管理 使用redis | ||||
| @@ -158,24 +161,24 @@ public class ShiroConfig { | |||||
| } | } | ||||
| @Bean | @Bean | ||||
| public MyShiroRealm myShiroRealm() { | |||||
| public MyShiroRealm myShiroRealm(MyRetryLimitCredentialsMatcher matcher) { | |||||
| MyShiroRealm myShiroRealm = new MyShiroRealm(); | MyShiroRealm myShiroRealm = new MyShiroRealm(); | ||||
| myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); | |||||
| myShiroRealm.setCredentialsMatcher(matcher); | |||||
| return myShiroRealm; | return myShiroRealm; | ||||
| } | } | ||||
| /** | /** | ||||
| * 凭证匹配器 | |||||
| * 密码匹配凭证管理器 凭证匹配器 | |||||
| * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 | * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 | ||||
| * 所以我们需要修改下doGetAuthenticationInfo中的代码; | * 所以我们需要修改下doGetAuthenticationInfo中的代码; | ||||
| * ) | * ) | ||||
| * | * | ||||
| * @return | * @return | ||||
| */ | */ | ||||
| @Bean | |||||
| public HashedCredentialsMatcher hashedCredentialsMatcher() { | |||||
| HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); | |||||
| @Bean(name = "myRetryLimitCredentialsMatcher") | |||||
| public MyRetryLimitCredentialsMatcher hashedCredentialsMatcher() { | |||||
| MyRetryLimitCredentialsMatcher hashedCredentialsMatcher = new MyRetryLimitCredentialsMatcher(); | |||||
| hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; | hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; | ||||
| hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5("")); | hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5("")); | ||||
| @@ -14,7 +14,11 @@ import com.iformall.service.MallRolePermissionService; | |||||
| import com.iformall.service.MallUserInfoService; | import com.iformall.service.MallUserInfoService; | ||||
| import com.iformall.service.MallUserRoleService; | import com.iformall.service.MallUserRoleService; | ||||
| import com.iformall.shiro.UserSession; | import com.iformall.shiro.UserSession; | ||||
| <<<<<<< HEAD | |||||
| import com.iformall.utils.IPUtil; | import com.iformall.utils.IPUtil; | ||||
| ======= | |||||
| import com.iformall.shiro.UseriFormallToken; | |||||
| >>>>>>> [ADMIN端领导免密登录][移动塔台]:领导手机授权免密登录 | |||||
| import com.iformall.utils.ShiroUtils; | import com.iformall.utils.ShiroUtils; | ||||
| import io.swagger.annotations.Api; | import io.swagger.annotations.Api; | ||||
| import io.swagger.annotations.ApiOperation; | import io.swagger.annotations.ApiOperation; | ||||
| @@ -144,7 +148,7 @@ public class HomeController extends BaseController { | |||||
| ResultData data = new ResultData(); | ResultData data = new ResultData(); | ||||
| if(StringUtils.isNotBlank(user.getPhone())) { | if(StringUtils.isNotBlank(user.getPhone())) { | ||||
| // 领导登录 | // 领导登录 | ||||
| user = mallUserInfoService.getByOpenId(user.getOpenId()); | |||||
| user = mallUserInfoService.getByPhone(user.getPhone()); | |||||
| if(!user.getIsAdmin().equals(EnumUserAdmin.ADMIN.getCode())) { | if(!user.getIsAdmin().equals(EnumUserAdmin.ADMIN.getCode())) { | ||||
| return new ResultData(ErrorCode.USER_NOT_ADMIN); | return new ResultData(ErrorCode.USER_NOT_ADMIN); | ||||
| } | } | ||||
| @@ -163,7 +167,7 @@ public class HomeController extends BaseController { | |||||
| try { | try { | ||||
| Subject subject = SecurityUtils.getSubject(); | Subject subject = SecurityUtils.getSubject(); | ||||
| UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword()); | |||||
| UseriFormallToken token = new UseriFormallToken(user.getUsername()); | |||||
| subject.login(token); | subject.login(token); | ||||
| // List<MallRole> roleList = sysRoleService.findRoleByUserId(user.getId()); | // List<MallRole> roleList = sysRoleService.findRoleByUserId(user.getId()); | ||||
| // if (null != roleList && roleList.size() > 0) { | // if (null != roleList && roleList.size() > 0) { | ||||
| @@ -0,0 +1,38 @@ | |||||
| package com.iformall.enums; | |||||
| /** | |||||
| * Created by Stormeye on 2018/11/16. | |||||
| */ | |||||
| public enum EnumLoginType { | |||||
| // 0-password, 1-nopassword | |||||
| PASSWORD(0, "PASSWORD"), | |||||
| NOPASSWD(1, "NOPASSWORD") | |||||
| ; | |||||
| public static EnumLoginType getEnum(Integer code) { | |||||
| for (EnumLoginType value : values()) { | |||||
| if (value.getCode().equals(code)) { | |||||
| return value; | |||||
| } | |||||
| } | |||||
| return null; | |||||
| } | |||||
| private Integer code; | |||||
| private String message; | |||||
| EnumLoginType(Integer code, String message) { | |||||
| this.code = code; | |||||
| this.message = message; | |||||
| } | |||||
| public Integer getCode() { | |||||
| return code; | |||||
| } | |||||
| public String getMessage() { | |||||
| return message; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,24 @@ | |||||
| package com.iformall.shiro; | |||||
| import com.iformall.enums.EnumLoginType; | |||||
| 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; | |||||
| @Configuration | |||||
| public class MyRetryLimitCredentialsMatcher extends HashedCredentialsMatcher { | |||||
| @Override | |||||
| public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) { | |||||
| if(authcToken instanceof UseriFormallToken) { | |||||
| UseriFormallToken tk = (UseriFormallToken) authcToken; | |||||
| if(tk.getType().equals(EnumLoginType.NOPASSWD)){ | |||||
| return true; | |||||
| } | |||||
| boolean matches = super.doCredentialsMatch(authcToken, info); | |||||
| return matches; | |||||
| } | |||||
| boolean matches =super.doCredentialsMatch(authcToken, info); | |||||
| return matches; | |||||
| } | |||||
| } | |||||
| @@ -2,6 +2,7 @@ package com.iformall.shiro; | |||||
| import javax.annotation.Resource; | import javax.annotation.Resource; | ||||
| import com.iformall.enums.EnumMallUserStatus; | |||||
| import com.iformall.service.MallUserInfoService; | import com.iformall.service.MallUserInfoService; | ||||
| import org.apache.shiro.SecurityUtils; | import org.apache.shiro.SecurityUtils; | ||||
| import org.apache.shiro.authc.AuthenticationException; | import org.apache.shiro.authc.AuthenticationException; | ||||
| @@ -51,11 +52,14 @@ public class MyShiroRealm extends AuthorizingRealm { | |||||
| //获取用户的输入的账号. | //获取用户的输入的账号. | ||||
| String username = (String)token.getPrincipal(); | String username = (String)token.getPrincipal(); | ||||
| MallUserInfo user = userService.getByUsername(username); | MallUserInfo user = userService.getByUsername(username); | ||||
| if(user==null) throw new UnknownAccountException("用户名不存在"); | |||||
| // if (0==user.getEnable()) { | |||||
| // throw new LockedAccountException(); // 帐号锁定 | |||||
| // } | |||||
| if(user.getStatus()==null || 1!=user.getStatus()) {//用户被禁用 | |||||
| if(user == null) { | |||||
| throw new UnknownAccountException("用户名不存在"); | |||||
| } | |||||
| // if (0==user.getEnable()) { | |||||
| // throw new LockedAccountException(); // 帐号锁定 | |||||
| // } | |||||
| if(user.getStatus()==null || | |||||
| !EnumMallUserStatus.VALID.getCode().equals(user.getStatus())) {//用户被禁用 | |||||
| throw new UnknownAccountException("用户被禁用"); | throw new UnknownAccountException("用户被禁用"); | ||||
| } | } | ||||
| SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo( | SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo( | ||||
| @@ -0,0 +1,39 @@ | |||||
| package com.iformall.shiro; | |||||
| import com.iformall.enums.EnumLoginType; | |||||
| import org.apache.shiro.authc.UsernamePasswordToken; | |||||
| public class UseriFormallToken extends UsernamePasswordToken { | |||||
| private static final long serialVersionUID = -2564928913725078138L; | |||||
| private EnumLoginType type; | |||||
| public UseriFormallToken() { | |||||
| super(); | |||||
| } | |||||
| public UseriFormallToken(String username, String password, EnumLoginType type, boolean rememberMe, String host) { | |||||
| super(username, password, rememberMe, host); | |||||
| this.type = type; | |||||
| } | |||||
| /** 免密登录 */ | |||||
| public UseriFormallToken(String username) { | |||||
| super(username, "", false, null); | |||||
| this.type = EnumLoginType.NOPASSWD; | |||||
| } | |||||
| /** 账号密码登录 */ | |||||
| public UseriFormallToken(String username, String pwd) { | |||||
| super(username, pwd, false, null); | |||||
| this.type = EnumLoginType.PASSWORD; | |||||
| } | |||||
| public EnumLoginType getType() { | |||||
| return type; | |||||
| } | |||||
| public void setType(EnumLoginType type) { | |||||
| this.type = type; | |||||
| } | |||||
| } | |||||