升级spring boot到2.0版本,修改shiro-tag对其版本的兼容
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -15,7 +15,7 @@
|
||||
<parent>
|
||||
<groupId>io.spring.platform</groupId>
|
||||
<artifactId>platform-bom</artifactId>
|
||||
<version>Brussels-SR5</version>
|
||||
<version>Cairo-RC1</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -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<SysMenuVO> menuVOList = JSONUtil.parseArray(SecurityUtils.getSubject().getSession().getAttribute(Constants.Session.MENU).toString(), SysMenuVO.class);
|
||||
Set<String> 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<String> menuSet, List<SysMenuVO> 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<SysMenuVO> menuVOList = JSONUtil.parseArray(SecurityUtils.getSubject().getSession().getAttribute(Constants.Session.MENU).toString(), SysMenuVO.class);
|
||||
Set<String> 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<String> menuSet, List<SysMenuVO> menuVOList) {
|
||||
if (CollectionUtil.isNotEmpty(menuVOList)) {
|
||||
for (SysMenuVO sm : menuVOList) {
|
||||
if (StringUtils.hasText(sm.getMenuUrl())) {
|
||||
menuSet.add(sm.getMenuUrl());
|
||||
}
|
||||
listMenu(menuSet, sm.getList());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,7 +20,9 @@
|
||||
|
||||
<br />
|
||||
<br />
|
||||
<@shiro.hasPermission name="/admin/sysUserInfo/add">
|
||||
<a href="${base}/admin/sysUserInfo/add" class="btn btn-default" data-toggle="dialog" data-icon="plus" data-id="sysUserInfo-add" data-options="{title:'添加', height:350}">添加 </a>
|
||||
</@shiro.hasPermission>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@@ -54,7 +56,9 @@
|
||||
<td>
|
||||
<a href="${base}/admin/sysUserInfo/view?id=${bean.id}" class="btn btn-blue" data-toggle="dialog" data-id="sysUserInfo-view" data-options="{title:'查看', height:350}">查看 </a>
|
||||
<a href="${base}/admin/sysUserInfo/edit?id=${bean.id}" class="btn btn-green" data-toggle="dialog" data-id="sysUserInfo-edit" data-options="{title:'修改', height:400}">修改 </a>
|
||||
<@shiro.hasPermission name="/admin/sysUserInfo/delete">
|
||||
<a href="${base}/admin/sysUserInfo/delete?id=${bean.id}" class="btn btn-red" data-toggle="doajax" data-id="sysUserInfo-delete" data-confirm-msg="确定要删除吗?">删除</a>
|
||||
</@shiro.hasPermission>
|
||||
<#if bean.userType == 1>
|
||||
<a href="${base}/admin/sysRoleUser/set?userInfoId=${bean.id}" class="btn btn-orange" data-toggle="dialog" data-id="sysUser-set" data-options="{title:'设置角色', height:350, width:800}">设置角色 </a>
|
||||
</#if>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user