diff --git a/pom.xml b/pom.xml index 48d6a37..60c642d 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ io.spring.platform platform-bom - Brussels-SR5 + Cairo-RC1 diff --git a/roncoo-recharge-boss/src/main/java/com/roncoo/recharge/boss/common/ShiroConfiguration.java b/roncoo-recharge-boss/src/main/java/com/roncoo/recharge/boss/common/ShiroConfiguration.java index ebe978a..c0c480c 100644 --- a/roncoo-recharge-boss/src/main/java/com/roncoo/recharge/boss/common/ShiroConfiguration.java +++ b/roncoo-recharge-boss/src/main/java/com/roncoo/recharge/boss/common/ShiroConfiguration.java @@ -3,126 +3,26 @@ */ package com.roncoo.recharge.boss.common; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - import javax.annotation.PostConstruct; -import org.apache.shiro.SecurityUtils; -import org.apache.shiro.authc.AuthenticationException; -import org.apache.shiro.authc.AuthenticationInfo; -import org.apache.shiro.authc.AuthenticationToken; -import org.apache.shiro.authc.SimpleAuthenticationInfo; -import org.apache.shiro.authc.UnknownAccountException; -import org.apache.shiro.authc.UsernamePasswordToken; -import org.apache.shiro.authz.AuthorizationInfo; -import org.apache.shiro.authz.SimpleAuthorizationInfo; -import org.apache.shiro.subject.PrincipalCollection; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration.FreeMarkerWebConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.util.StringUtils; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; -import com.roncoo.recharge.boss.bean.vo.SysMenuVO; -import com.roncoo.recharge.common.dao.SysUserInfoDao; -import com.roncoo.recharge.common.entity.SysUserInfo; -import com.roncoo.recharge.util.Constants; -import com.roncoo.recharge.util.JSONUtil; import com.roncoo.shiro.freemarker.ShiroTags; -import com.roncoo.spring.boot.autoconfigure.shiro.ShiroRealm; -import com.xiaoleilu.hutool.crypto.DigestUtil; -import com.xiaoleilu.hutool.util.CollectionUtil; /** * shiro配置类 */ -@Configuration -public class ShiroConfiguration extends FreeMarkerWebConfiguration { +@Component +public class ShiroConfiguration { @Autowired - private freemarker.template.Configuration configuration; + private FreeMarkerConfigurer freeMarkerConfigurer; @PostConstruct public void setSharedVariable() { - try { - configuration.setSharedVariable("shiro", new ShiroTags()); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * ShiroRealm - */ - @Bean(name = "shiroRealm") - public ShiroRealm shiroRealm() { - ShiroCustomRealm realm = new ShiroCustomRealm(); - return realm; - } - -} - -class ShiroCustomRealm extends ShiroRealm { - - protected final Logger logger = LoggerFactory.getLogger(this.getClass()); - - @Autowired - private SysUserInfoDao sysUserInfoDao; - - /** - * 授权认证 - */ - @Override - protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) { - SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); - List menuVOList = JSONUtil.parseArray(SecurityUtils.getSubject().getSession().getAttribute(Constants.Session.MENU).toString(), SysMenuVO.class); - Set menuSet = new HashSet<>(); - // 处理菜单权限 - listMenu(menuSet, menuVOList); - simpleAuthorizationInfo.setStringPermissions(menuSet); - return simpleAuthorizationInfo; - } - - /** - * 登录认证 - */ - @Override - protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException { - UsernamePasswordToken token = (UsernamePasswordToken) arg0; - //logger.info(ReflectionToStringBuilder.toString(token, ToStringStyle.MULTI_LINE_STYLE)); - - SysUserInfo sysUserInfo = sysUserInfoDao.getByLoginName(token.getUsername()); - if (null == sysUserInfo) { - throw new UnknownAccountException("账号或者密码不正确"); - } - - if (!sysUserInfo.getPwd().equals(DigestUtil.md5Hex(sysUserInfo.getSalt() + new String(token.getPassword())))) { - throw new UnknownAccountException("账号或者密码不正确"); - } - - SecurityUtils.getSubject().getSession().setAttribute(Constants.Session.USER_TYPE, sysUserInfo.getUserType()); - SecurityUtils.getSubject().getSession().setAttribute(Constants.Session.USER_ID, sysUserInfo.getId()); - SecurityUtils.getSubject().getSession().setAttribute(Constants.Session.USER, JSONUtil.toJSONString(sysUserInfo)); - return new SimpleAuthenticationInfo(token.getUsername(), token.getPassword(), getName()); - } - - /** - * @param list - * @return - */ - private static void listMenu(Set menuSet, List menuVOList) { - if (CollectionUtil.isNotEmpty(menuVOList)) { - for (SysMenuVO sm : menuVOList) { - if (StringUtils.hasText(sm.getMenuUrl())) { - menuSet.add(sm.getMenuUrl()); - } - listMenu(menuSet, sm.getList()); - } - } + freeMarkerConfigurer.getConfiguration().setSharedVariable("shiro", new ShiroTags()); } } diff --git a/roncoo-recharge-boss/src/main/java/com/roncoo/recharge/boss/common/WebMvcConfigurer.java b/roncoo-recharge-boss/src/main/java/com/roncoo/recharge/boss/common/WebMvcConfigurer.java index 176d354..b1cac76 100644 --- a/roncoo-recharge-boss/src/main/java/com/roncoo/recharge/boss/common/WebMvcConfigurer.java +++ b/roncoo-recharge-boss/src/main/java/com/roncoo/recharge/boss/common/WebMvcConfigurer.java @@ -12,37 +12,117 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.AuthenticationInfo; +import org.apache.shiro.authc.AuthenticationToken; +import org.apache.shiro.authc.SimpleAuthenticationInfo; +import org.apache.shiro.authc.UnknownAccountException; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.authz.AuthorizationInfo; +import org.apache.shiro.authz.SimpleAuthorizationInfo; +import org.apache.shiro.subject.PrincipalCollection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.StringUtils; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.roncoo.recharge.boss.bean.vo.SysMenuVO; +import com.roncoo.recharge.common.dao.SysUserInfoDao; +import com.roncoo.recharge.common.entity.SysUserInfo; import com.roncoo.recharge.util.Constants; import com.roncoo.recharge.util.JSONUtil; import com.roncoo.recharge.util.base.BaseRoncoo; import com.roncoo.recharge.util.bjui.Bjui; +import com.roncoo.spring.boot.autoconfigure.shiro.ShiroRealm; +import com.xiaoleilu.hutool.crypto.DigestUtil; import com.xiaoleilu.hutool.util.CollectionUtil; /** * 拦截器 */ @Configuration -public class WebMvcConfigurer extends WebMvcConfigurerAdapter { +public class WebMvcConfigurer { + + /** + * ShiroRealm + */ + @Bean(name = "shiroRealm") + public ShiroRealm shiroRealm() { + ShiroCustomRealm realm = new ShiroCustomRealm(); + return realm; + } @Bean ShiroInterceptor shiroInterceptor() { return new ShiroInterceptor(); } +} + +class ShiroCustomRealm extends ShiroRealm { + + protected final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Autowired + private SysUserInfoDao sysUserInfoDao; + + /** + * 授权认证 + */ @Override - public void addInterceptors(InterceptorRegistry registry) { - //registry.addInterceptor(shiroInterceptor()).addPathPatterns("/admin/**"); - super.addInterceptors(registry); + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) { + SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); + List menuVOList = JSONUtil.parseArray(SecurityUtils.getSubject().getSession().getAttribute(Constants.Session.MENU).toString(), SysMenuVO.class); + Set menuSet = new HashSet<>(); + // 处理菜单权限 + listMenu(menuSet, menuVOList); + simpleAuthorizationInfo.setStringPermissions(menuSet); + return simpleAuthorizationInfo; } + + /** + * 登录认证 + */ + @Override + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException { + UsernamePasswordToken token = (UsernamePasswordToken) arg0; + // logger.info(ReflectionToStringBuilder.toString(token, + // ToStringStyle.MULTI_LINE_STYLE)); + + SysUserInfo sysUserInfo = sysUserInfoDao.getByLoginName(token.getUsername()); + if (null == sysUserInfo) { + throw new UnknownAccountException("账号或者密码不正确"); + } + + if (!sysUserInfo.getPwd().equals(DigestUtil.md5Hex(sysUserInfo.getSalt() + new String(token.getPassword())))) { + throw new UnknownAccountException("账号或者密码不正确"); + } + + SecurityUtils.getSubject().getSession().setAttribute(Constants.Session.USER_TYPE, sysUserInfo.getUserType()); + SecurityUtils.getSubject().getSession().setAttribute(Constants.Session.USER_ID, sysUserInfo.getId()); + SecurityUtils.getSubject().getSession().setAttribute(Constants.Session.USER, JSONUtil.toJSONString(sysUserInfo)); + return new SimpleAuthenticationInfo(token.getUsername(), token.getPassword(), getName()); + } + + /** + * @param list + * @return + */ + private static void listMenu(Set menuSet, List menuVOList) { + if (CollectionUtil.isNotEmpty(menuVOList)) { + for (SysMenuVO sm : menuVOList) { + if (StringUtils.hasText(sm.getMenuUrl())) { + menuSet.add(sm.getMenuUrl()); + } + listMenu(menuSet, sm.getList()); + } + } + } + } /** diff --git a/roncoo-recharge-boss/src/main/resources/templates/admin/sysUserInfo/list.ftl b/roncoo-recharge-boss/src/main/resources/templates/admin/sysUserInfo/list.ftl index 35c6245..9732404 100644 --- a/roncoo-recharge-boss/src/main/resources/templates/admin/sysUserInfo/list.ftl +++ b/roncoo-recharge-boss/src/main/resources/templates/admin/sysUserInfo/list.ftl @@ -20,7 +20,9 @@

+ <@shiro.hasPermission name="/admin/sysUserInfo/add"> 添加 + @@ -54,7 +56,9 @@ 查看 修改 + <@shiro.hasPermission name="/admin/sysUserInfo/delete"> 删除 + <#if bean.userType == 1> 设置角色 diff --git a/roncoo-recharge-gateway/src/main/java/com/roncoo/recharge/gateway/common/WebMvcConfigurer.java b/roncoo-recharge-gateway/src/main/java/com/roncoo/recharge/gateway/common/WebMvcConfigurer.java index 929ab29..cd2f32e 100644 --- a/roncoo-recharge-gateway/src/main/java/com/roncoo/recharge/gateway/common/WebMvcConfigurer.java +++ b/roncoo-recharge-gateway/src/main/java/com/roncoo/recharge/gateway/common/WebMvcConfigurer.java @@ -10,8 +10,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.roncoo.recharge.util.base.BaseRoncoo; import com.xiaoleilu.hutool.http.HttpUtil; @@ -20,18 +18,13 @@ import com.xiaoleilu.hutool.http.HttpUtil; * 拦截器 */ @Configuration -public class WebMvcConfigurer extends WebMvcConfigurerAdapter { +public class WebMvcConfigurer { @Bean ShiroInterceptor shiroInterceptor() { return new ShiroInterceptor(); } - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(shiroInterceptor()).addPathPatterns("/boss/**"); - super.addInterceptors(registry); - } } /** @@ -43,7 +36,7 @@ class ShiroInterceptor extends BaseRoncoo implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 校验请求的ip System.out.println(HttpUtil.getClientIP(request)); - + return true; }