mirror of
https://github.com/RemainderTime/spring-boot-base-demo.git
synced 2026-03-02 01:50:44 +08:00
实现程序内静态页面跳转
实现dockerfile文件
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package cn.xf.basedemo.common.utils;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @program: xf-boot-base
|
||||
* @ClassName ApplicationContextUtils
|
||||
* @description:
|
||||
* @author: xiongfeng
|
||||
* @create: 2022-07-06 14:13
|
||||
**/
|
||||
@Component
|
||||
public class ApplicationContextUtils implements ApplicationContextAware {
|
||||
//放置在获取bean的时候提示空指针,将其定义为静态变量
|
||||
private static ApplicationContext context;
|
||||
|
||||
//类初始化完成之后调用setApplicationContext()方法进行操作
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
ApplicationContextUtils.context = applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext geteContext(){
|
||||
return context;
|
||||
}
|
||||
|
||||
public static Object getBean(String beanName){
|
||||
//在这一步的时候一定要注意,此时可调用这个方法的时候
|
||||
//context可能为空,会提示空指针异常,需要将其定义成静态的,这样类加载的时候
|
||||
//context就已经存在了
|
||||
return context.getBean(beanName);
|
||||
}
|
||||
}
|
||||
42
src/main/java/cn/xf/basedemo/controller/LoginController.java
Normal file
42
src/main/java/cn/xf/basedemo/controller/LoginController.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package cn.xf.basedemo.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* @program: xf-boot-base
|
||||
* @ClassName LoginController
|
||||
* @description: 页面跳转转发控制器
|
||||
* @author: xiongfeng
|
||||
* @create: 2022-07-06 11:51
|
||||
**/
|
||||
@Controller
|
||||
@RequestMapping("web")
|
||||
public class LoginController {
|
||||
|
||||
/**
|
||||
* @Description 进入登录页面
|
||||
* @Author xiongfeng
|
||||
* @Date 2022/7/6
|
||||
* @Param []
|
||||
* @return java.lang.String
|
||||
**/
|
||||
@RequestMapping(value = "/login")
|
||||
public String login() {
|
||||
return "login";
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 静态页面跳转统一入口
|
||||
* @Author xiongfeng
|
||||
* @Date 2022/7/6
|
||||
* @Param [url]
|
||||
* @return java.lang.String
|
||||
**/
|
||||
@RequestMapping(value = "/{url}")
|
||||
public String skipUrl(@PathVariable(name = "url") String url) {
|
||||
return url;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
/**
|
||||
* @program: xf-boot-base
|
||||
* @ClassName UserController
|
||||
* @description:
|
||||
* @description: 用户控制器
|
||||
* @author: xiongfeng
|
||||
* @create: 2022-06-28 09:17
|
||||
**/
|
||||
|
||||
@@ -19,6 +19,6 @@ public class InterceptorConfig implements WebMvcConfigurer {
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new TokenInterceptor()) //登录逻辑拦截类
|
||||
.addPathPatterns("/**") //需要拦截的请求(设置的全部拦截)
|
||||
.excludePathPatterns("`/user/login","/user/register`"); //忽略的请求
|
||||
.excludePathPatterns("/user/**","/web/**"); //忽略的请求
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package cn.xf.basedemo.interceptor;
|
||||
|
||||
import cn.xf.basedemo.common.model.LoginUser;
|
||||
import cn.xf.basedemo.common.utils.ApplicationContextUtils;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
@@ -22,15 +24,15 @@ import java.util.concurrent.TimeUnit;
|
||||
**/
|
||||
public class TokenInterceptor implements HandlerInterceptor {
|
||||
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
RedisTemplate<Object,Object> redisTemplate =
|
||||
(RedisTemplate<Object,Object>) ApplicationContextUtils.getBean("redisTemplate");
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
//不拦截的请求列表
|
||||
private static final List<String> EXCLUDE_PATH_LIST = Arrays.asList("/user/login");
|
||||
private static final List<String> EXCLUDE_PATH_LIST = Arrays.asList("/user/login","/web/login");
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
@@ -43,8 +45,10 @@ public class TokenInterceptor implements HandlerInterceptor {
|
||||
String token = request.getHeader("Authorization");
|
||||
if(StringUtils.isEmpty(token))
|
||||
token = request.getParameter("token");
|
||||
|
||||
String value = redisTemplate.opsForValue().get(token);
|
||||
if(StringUtils.isEmpty(token)){
|
||||
return false;
|
||||
}
|
||||
String value = (String) redisTemplate.opsForValue().get("token:" + token);
|
||||
if(StringUtils.isEmpty(value)){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ public class UserServiceImpl implements UserService {
|
||||
loginInfo = objectMapper.readValue(loginJson, LoginInfo.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
return RetObj.error("账号或密码错误");
|
||||
}
|
||||
if (StringUtils.isNotBlank(loginInfo.check())) {
|
||||
return RetObj.error(loginInfo.check());
|
||||
|
||||
Reference in New Issue
Block a user