parent
50d0a855c9
commit
1d8fd46bee
5 changed files with 109 additions and 0 deletions
@ -0,0 +1,29 @@ |
|||||||
|
package com.yipin.liuwanr.config; |
||||||
|
|
||||||
|
/** |
||||||
|
* @description |
||||||
|
* @author: Mr.JK |
||||||
|
* @create: 2021-06-03 16:24 |
||||||
|
**/ |
||||||
|
|
||||||
|
import com.yipin.liuwanr.filter.AuthInterceptor; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
public class AuthConfig implements WebMvcConfigurer { |
||||||
|
|
||||||
|
@Bean |
||||||
|
public AuthInterceptor initAuthInterceptor(){ |
||||||
|
return new AuthInterceptor(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void addInterceptors(InterceptorRegistry registry) { |
||||||
|
registry.addInterceptor(initAuthInterceptor()).addPathPatterns("/**") |
||||||
|
.excludePathPatterns("/userInfo/adminLogins/**","/userInfo/loginSchoolClient/**","/userInfo/logins/**","/userInfo/updateLogInNumber/**"); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,58 @@ |
|||||||
|
package com.yipin.liuwanr.filter; |
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate; |
||||||
|
import org.springframework.data.redis.core.ValueOperations; |
||||||
|
import org.springframework.util.StringUtils; |
||||||
|
import org.springframework.web.servlet.HandlerInterceptor; |
||||||
|
import org.springframework.web.servlet.ModelAndView; |
||||||
|
|
||||||
|
import javax.annotation.Resource; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
import java.util.Objects; |
||||||
|
import java.util.concurrent.TimeUnit; |
||||||
|
|
||||||
|
/** |
||||||
|
* @description |
||||||
|
* @author: Mr.JK |
||||||
|
* @create: 2021-06-03 16:19 |
||||||
|
**/ |
||||||
|
public class AuthInterceptor implements HandlerInterceptor { |
||||||
|
|
||||||
|
@Resource |
||||||
|
StringRedisTemplate stringRedisTemplate; |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
||||||
|
response.setCharacterEncoding("UTF-8"); |
||||||
|
response.setContentType("text/html;charset=utf-8"); |
||||||
|
String token = request.getHeader("token"); |
||||||
|
if (StringUtils.isEmpty(token)) { |
||||||
|
response.getWriter().print("0");//用户未登录,请登录后操作!
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue(); |
||||||
|
Object loginStatus = ops.get(token); |
||||||
|
|
||||||
|
|
||||||
|
if( Objects.isNull(loginStatus)){ |
||||||
|
response.getWriter().print("1");//token错误
|
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue