| | |
| | | package org.dromara.common.security.config; |
| | | |
| | | import cn.dev33.satoken.exception.NotLoginException; |
| | | import cn.dev33.satoken.filter.SaServletFilter; |
| | | import cn.dev33.satoken.httpauth.basic.SaHttpBasicUtil; |
| | | import cn.dev33.satoken.interceptor.SaInterceptor; |
| | | import cn.dev33.satoken.router.SaRouter; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import cn.dev33.satoken.util.SaResult; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.core.constant.HttpStatus; |
| | | import org.dromara.common.core.utils.ServletUtils; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.common.core.context.ThreadLocalHolder; |
| | | import org.dromara.common.security.config.properties.SecurityProperties; |
| | | import org.dromara.common.security.handler.AllUrlHandler; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | |
| | |
| | | StpUtil.getTokenValue()); |
| | | } |
| | | |
| | | // 保存用户信息 |
| | | ThreadLocalHolder.set(LoginHelper.LOGIN_USER_KEY, LoginHelper.getLoginUser()); |
| | | |
| | | // 有效率影响 用于临时测试 |
| | | // if (log.isDebugEnabled()) { |
| | | // log.info("剩余有效时间: {}", StpUtil.getTokenTimeout()); |
| | |
| | | // } |
| | | |
| | | }); |
| | | }) |
| | | { |
| | | @Override |
| | | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { |
| | | ThreadLocalHolder.remove(LoginHelper.LOGIN_USER_KEY); |
| | | } |
| | | }).addPathPatterns("/**") |
| | | })).addPathPatterns("/**") |
| | | // 排除不需要拦截的路径 |
| | | .excludePathPatterns(securityProperties.getExcludes()); |
| | | } |
| | | |
| | | /** |
| | | * 对 actuator 健康检查接口 做账号密码鉴权 |
| | | */ |
| | | @Bean |
| | | public SaServletFilter getSaServletFilter() { |
| | | String username = SpringUtils.getProperty("spring.boot.admin.client.username"); |
| | | String password = SpringUtils.getProperty("spring.boot.admin.client.password"); |
| | | return new SaServletFilter() |
| | | .addInclude("/actuator", "/actuator/**") |
| | | .setAuth(obj -> { |
| | | SaHttpBasicUtil.check(username + ":" + password); |
| | | }) |
| | | .setError(e -> SaResult.error(e.getMessage()).setCode(HttpStatus.UNAUTHORIZED)); |
| | | } |
| | | |
| | | } |