This commit is contained in:
13405411873 2023-10-25 14:40:20 +08:00
parent 36687535b7
commit b0b04f4349
7 changed files with 96 additions and 85 deletions

View File

@ -1,10 +1,11 @@
package com.fuint.common.config;
import com.fuint.common.web.AdminUserInterceptor;
import com.fuint.common.web.CommandInterceptor;
import com.fuint.common.web.ClientUserInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.CacheControl;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.*;
@ -20,16 +21,20 @@ import java.util.concurrent.TimeUnit;
*/
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Autowired
private Environment env;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String pathRoot = env.getProperty("images.root");
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/", "classpath:/other-resources/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
.resourceChain(false)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new CssLinkResourceTransformer());
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/static/**") .addResourceLocations("file:" + pathRoot + "/static/");
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
@ -40,10 +45,6 @@ public class WebConfig extends WebMvcConfigurationSupport {
super.addResourceHandlers(registry);
}
@Bean
public CommandInterceptor commandInterceptor() {
return new CommandInterceptor();
}
@Bean
public AdminUserInterceptor adminUserInterceptor() {
@ -57,9 +58,6 @@ public class WebConfig extends WebMvcConfigurationSupport {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// Command
registry.addInterceptor(commandInterceptor())
.addPathPatterns("/cmd/**");
// 后台拦截
registry.addInterceptor(adminUserInterceptor())

View File

@ -0,0 +1,81 @@
package com.fuint.common.shiroConfig;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fuint.common.dto.AccountInfo;
import com.fuint.common.util.Constants;
import com.fuint.common.util.RedisUtil;
import com.fuint.common.util.TokenUtil;
import com.fuint.framework.FrameworkConstants;
import com.fuint.framework.web.ResponseObject;
import com.fuint.system.dept.entity.SysDept;
import com.fuint.system.dept.service.ISysDeptService;
import com.fuint.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
@Slf4j
public class CommonFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
try {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String token = request.getHeader("Access-Token");
Object loginInfo = RedisUtil.get(Constants.SESSION_ADMIN_USER + token);
ObjectMapper objectMapper = new ObjectMapper();
AccountInfo nowAccountInfo = objectMapper.convertValue(loginInfo, AccountInfo.class);
if (ObjectUtil.isNotEmpty(nowAccountInfo)) {
ISysDeptService deptService = SpringUtils.getBean(ISysDeptService.class);
//判断机构的状态
SysDept dept = deptService.getById(nowAccountInfo.getDeptId());
if (dept.getStatus().equals("qy")){
filterChain.doFilter(servletRequest, servletResponse);
}else {
ResponseObject responseObject = new ResponseObject(444, "机构禁用", "");
returnJson(servletResponse, JSONObject.toJSONString(responseObject));
}
}else {
ResponseObject responseObject = new ResponseObject(401, "登录信息已失效,请重新登录", "");
returnJson(servletResponse, JSONObject.toJSONString(responseObject));
}
}catch (Exception e){
log.error(e.getMessage());
}
}
/**
* 封装异常返回数据
* @param response
* @param json
* @throws Exception
*/
private void returnJson(ServletResponse response, String json) throws Exception{
PrintWriter writer = null;
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
try {
writer = response.getWriter();
writer.print(json);
} catch (IOException e) {
log.error("response error",e);
} finally {
if (writer != null) {
writer.close();
}
}
}
}

View File

@ -1,18 +1,16 @@
package com.fuint.framework.shiroConfig;
package com.fuint.common.shiroConfig;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
@ -28,15 +26,14 @@ public class ShiroConfig {
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
ShiroFilterFactoryBean filter=new ShiroFilterFactoryBean();
filter.setSecurityManager(securityManager);
//设置shiro的拦截规则
//anon 匿名用户可访问 authc 认证用户可访问
//user 使用RemeberMe的用户可访问 perms 对应权限可访问
//role 对应的角色可访问
Map<String, Filter> filters =new HashMap<>();
filters.put("commonFilter",new CommonFilter());
filter.setFilters(filters);
LinkedHashMap<String,String> filterMap=new LinkedHashMap<>();
filterMap.put("/backendApi/login/doLogin","anon");
filterMap.put("/clientApi/captcha/getCode","anon");
filterMap.put("/static/**","anon");
filterMap.put("/**","authc");
filterMap.put("/**","commonFilter");
filter.setFilterChainDefinitionMap(filterMap);
filter.setLoginUrl("/login");
return filter;

View File

@ -1,4 +1,4 @@
package com.fuint.framework.shiroConfig;
package com.fuint.common.shiroConfig;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

View File

@ -1,23 +0,0 @@
package com.fuint.common.web;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by FSQ
* CopyRight https://www.fuint.cn
*/
public class CommandInterceptor implements AsyncHandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String serverName = request.getServerName();
if (!StringUtils.isEmpty(serverName) && serverName.equals("localhost")) {
return true;
} else {
return false;
}
}
}

View File

@ -1,37 +0,0 @@
package com.fuint.common.web;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
/**
* Created by FSQ
* CopyRight https://www.fuint.cn
*/
@Service
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextHolder.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
public static <T> T getBean(String beanName, Class<T> clazz) {
return applicationContext.getBean(beanName, clazz);
}
}

View File

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite use-query-string="true">
</urlrewrite>