mdf:项目结构修改
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package org.nl.sso;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2022/11/24 4:06 下午
|
||||
*/
|
||||
//@ComponentScan("org.nl.sso.*")
|
||||
@SpringBootApplication
|
||||
public class AutoConfigurationEntry {
|
||||
static {
|
||||
System.out.println("sso-client-service runing");}
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AutoConfigurationEntry.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.nl.sso.base;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 跨域过滤器
|
||||
* @author kong
|
||||
*/
|
||||
@Component
|
||||
@Order(-200)
|
||||
public class CorsFilter implements Filter {
|
||||
|
||||
static final String OPTIONS = "OPTIONS";
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
HttpServletRequest request = (HttpServletRequest) req;
|
||||
HttpServletResponse response = (HttpServletResponse) res;
|
||||
|
||||
// 允许指定域访问跨域资源
|
||||
response.setHeader("Access-Control-Allow-Origin", "*");
|
||||
// 允许所有请求方式
|
||||
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
|
||||
// 有效时间
|
||||
response.setHeader("Access-Control-Max-Age", "3600");
|
||||
// 允许的header参数
|
||||
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,satoken");
|
||||
|
||||
// 如果是预检请求,直接返回
|
||||
if (OPTIONS.equals(request.getMethod())) {
|
||||
System.out.println("=======================浏览器发来了OPTIONS预检请求==========");
|
||||
response.getWriter().print("");
|
||||
return;
|
||||
}
|
||||
|
||||
// System.out.println("*********************************过滤器被使用**************************");
|
||||
chain.doFilter(req, res);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.nl.sso.base;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.config.SaSsoConfig;
|
||||
import cn.dev33.satoken.sso.SaSsoManager;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSON;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.nl.sso.base.dto.UserEntry;
|
||||
import org.nl.sso.base.util.UserUtil;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2022/11/26 10:12 上午
|
||||
*/
|
||||
public class LoginInterceptor implements HandlerInterceptor {
|
||||
private List whileUrl= Arrays.asList("auth/login","/error","/logout","auth/sso/logoutCall","/auth/logout");
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
if (whileUrl.contains(request.getRequestURI())){
|
||||
return true;
|
||||
}
|
||||
String loginId = StpUtil.getLoginId(null);
|
||||
System.out.println("-- 登入请求---"+loginId);
|
||||
if (loginId ==null){
|
||||
return false;
|
||||
}else {
|
||||
UserEntry user = UserUtil.userCache.get(loginId);
|
||||
if (user ==null){
|
||||
//查询数据库
|
||||
HttpResponse execute = HttpRequest.get(SaSsoManager.getConfig().userinfoUrl + "?loginId=" + loginId).execute();
|
||||
if (200==execute.getStatus()){
|
||||
String body = execute.body();
|
||||
UserEntry info = new UserEntry();
|
||||
info.setTotalInfo(body);
|
||||
info.setUserId(Long.valueOf(loginId));
|
||||
UserUtil.setUserEntry(info);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.nl.sso.base.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2022/11/25 5:44 下午
|
||||
*/
|
||||
public class UserEntry implements Serializable {
|
||||
private Long userId;
|
||||
private String userName;
|
||||
private String totalInfo;
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getTotalInfo() {
|
||||
return totalInfo;
|
||||
}
|
||||
|
||||
public void setTotalInfo(String totalInfo) {
|
||||
this.totalInfo = totalInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.nl.sso.base.saconfig;
|
||||
|
||||
import cn.dev33.satoken.config.SaSsoConfig;
|
||||
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.dtflys.forest.Forest;
|
||||
import lombok.Data;
|
||||
import org.nl.sso.base.LoginInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2022/11/24 3:55 下午
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "sa-token.sso")
|
||||
public class BaseConfig implements WebMvcConfigurer {
|
||||
|
||||
private String pathPatterns ="/**";
|
||||
private String projectName ="上海诺力欢迎你";
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 注册 Sa-Token 拦截器,打开注解式鉴权功能
|
||||
registry.addInterceptor(new SaInterceptor()).addPathPatterns(pathPatterns);
|
||||
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void configSso(SaSsoConfig sso) {
|
||||
// 配置Http请求处理器
|
||||
sso.setSendHttp(url -> {
|
||||
HttpResponse execute = HttpRequest.get(url).execute();
|
||||
return execute.body();
|
||||
});
|
||||
}
|
||||
|
||||
public String getPathPatterns() {
|
||||
return pathPatterns;
|
||||
}
|
||||
|
||||
public void setPathPatterns(String pathPatterns) {
|
||||
this.pathPatterns = pathPatterns;
|
||||
}
|
||||
|
||||
public String getProjectName() {
|
||||
return projectName;
|
||||
}
|
||||
|
||||
public void setProjectName(String projectName) {
|
||||
this.projectName = projectName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.nl.sso.base.saconfig;
|
||||
|
||||
import cn.dev33.satoken.stp.StpInterface;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2022/11/24 3:59 下午
|
||||
* 权限系统处理类
|
||||
*/
|
||||
@Component
|
||||
public final class PermissionHandler implements StpInterface {
|
||||
@Override
|
||||
public List<String> getPermissionList(Object loginId, String loginType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getRoleList(Object loginId, String loginType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.nl.sso.base.util;
|
||||
|
||||
import org.nl.sso.base.dto.UserEntry;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2022/11/25 5:44 下午
|
||||
*/
|
||||
public class UserUtil {
|
||||
|
||||
public static Map<Long,UserEntry> userCache = new HashMap<>();
|
||||
|
||||
private static final ThreadLocal<UserEntry> holder = new ThreadLocal<>();
|
||||
|
||||
public static UserEntry getUserInfo() {
|
||||
return holder.get();
|
||||
}
|
||||
|
||||
public static void setUserEntry(UserEntry userInfo) {
|
||||
holder.set(userInfo);
|
||||
}
|
||||
|
||||
public static void remove() {
|
||||
holder.remove();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.nl.sso.controller;
|
||||
|
||||
import cn.dev33.satoken.sso.SaSsoManager;
|
||||
import cn.dev33.satoken.sso.SaSsoProcessor;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
import org.nl.sso.base.util.UserUtil;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2022/11/26 10:05 上午
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("auth")
|
||||
public class LoginController {
|
||||
|
||||
//首页登入测试页面
|
||||
@RequestMapping("/login")
|
||||
public ModelAndView login(ModelAndView mv) {
|
||||
String authUrl = SaSsoManager.getConfig().splicingAuthUrl();
|
||||
mv.setViewName("login");
|
||||
mv.addObject("title","上海诺力欢迎你");
|
||||
mv.addObject("authUrl",authUrl+"?mode=simple&redirect=http://localhost:8080/home");
|
||||
mv.addObject("isLogin",StpUtil.isLogin());
|
||||
return mv;
|
||||
}
|
||||
//获取用户信息:包括权限角色菜单等
|
||||
@RequestMapping("/info")
|
||||
public String userInfo() {
|
||||
String totalInfo = UserUtil.getUserInfo().getTotalInfo();
|
||||
return totalInfo;
|
||||
}
|
||||
//首页登入测试页面
|
||||
@RequestMapping("/logout")
|
||||
public ResponseEntity logout() {
|
||||
SaSsoProcessor.instance.ssoLogout();
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping("/sso/logoutCall")
|
||||
public void logoutCall() {
|
||||
System.out.println("-----"+"服务端回调注销");
|
||||
SaSsoProcessor.instance.ssoLogoutCall();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user