doum
7 小时以前 80fd41ea0dc602ac3ca33778f17fce5bc2e817b1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.doumee.config.Jwt;
 
import com.doumee.config.annotation.LoginRequired;
import com.doumee.config.annotation.LoginShopRequired;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.utils.Constants;
import io.jsonwebtoken.JwtException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
 
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
 
 
    @Autowired
    private JdbcTemplate dao;
 
    /**
     * 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //API接口JwtToken拦截器
        HandlerInterceptor TokenInterceptor = new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                // 如果不是映射到方法直接通过
                if (!(handler instanceof HandlerMethod)) {
                    return true;
                }
                HandlerMethod handlerMethod = (HandlerMethod) handler;
 
                Class<?> beanType = handlerMethod.getBeanType();
 
//                Method method = handlerMethod.getMethod();
 
                // 有 @LoginRequired 注解,需要登录认证
                if (beanType.isAnnotationPresent(LoginRequired.class)) {
                    //获取token
                    String token = request.getHeader(JwtTokenUtil.HEADER_KEY);  // 从 http 请求头中取出 token
                    if (StringUtils.isNotBlank(token)) {
                        checkMemberLogin(request,response);
                    } else {
                        throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录");
                    }
                }else if (handlerMethod.hasMethodAnnotation(LoginRequired.class)){
                    //获取token
                    String token = request.getHeader(JwtTokenUtil.HEADER_KEY);  // 从 http 请求头中取出 token
                    if (StringUtils.isNotBlank(token)) {
                        checkMemberLogin(request,response);
                    } else {
                        throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录");
                    }
                }else if (beanType.isAnnotationPresent(LoginShopRequired.class)) {
                    //获取token
                    String token = request.getHeader(JwtTokenUtil.HEADER_KEY);  // 从 http 请求头中取出 token
                    if (StringUtils.isNotBlank(token)) {
                        checkShopLogin(request,response);
                    } else {
                        throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录");
                    }
                }else if (handlerMethod.hasMethodAnnotation(LoginShopRequired.class)){
                    //获取token
                    String token = request.getHeader(JwtTokenUtil.HEADER_KEY);  // 从 http 请求头中取出 token
                    if (StringUtils.isNotBlank(token)) {
                        checkShopLogin(request,response);
                    } else {
                        throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录");
                    }
                }
 
 
                return true;
            }
        };
        registry.addInterceptor(TokenInterceptor).addPathPatterns("/web/**");
    }
 
 
 
    public Boolean checkMemberLogin(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
        try {
            //判断Token是否超时
            boolean expiration = JwtTokenUtil.isTokenExpired(token);
            if (expiration) {
                throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"长时间未操作,请重新登录");
            }
            //获取账号ID
            String memberIdInfo = JwtTokenUtil.getJwtPayLoad(token).getMemberId();
            if(StringUtils.isBlank(memberIdInfo)||!memberIdInfo.startsWith(Constants.MEMBER_PREFIX)){
                throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"用户信息出错");
            }
            Integer memberId = Integer.valueOf(memberIdInfo.replace(Constants.MEMBER_PREFIX,""));
            Integer isDeleted = dao.queryForObject(" select COALESCE(ISDELETED,0)  from Member where id  = ?", Integer.class, memberId);
            if(isDeleted== Constants.ONE){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已删除,请联系管理员");
            }
            Integer isForbidden = dao.queryForObject(" select COALESCE(STATUS,0)  from Member where id  = ?", Integer.class, memberId);
            if(isForbidden== Constants.ONE){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已禁用,请联系管理员");
            }
            Integer count = dao.queryForObject("select count(1) from Member where id  = ?", Integer.class, memberId);
            if (count != null && count > 0) {
                request.setAttribute(JwtTokenUtil.UserId_Name, memberId);
                return true;
            }else{
                throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"用户信息出错");
            }
        } catch (IllegalArgumentException | JwtException e) {
            throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录");
        }
    }
 
 
    public Boolean checkShopLogin(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
        try {
            //判断Token是否超时
            boolean expiration = JwtTokenUtil.isTokenExpired(token);
            if (expiration) {
                throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"长时间未操作,请重新登录");
            }
            //获取账号ID
            String shopInfo = JwtTokenUtil.getJwtPayLoad(token).getMemberId();
            if(StringUtils.isBlank(shopInfo)||!shopInfo.startsWith(Constants.SHOP_PREFIX)){
                throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"用户信息出错");
            }
            Integer shopId = Integer.valueOf(shopInfo.replace(Constants.SHOP_PREFIX,""));
            Integer isDeleted = dao.queryForObject(" select COALESCE(ISDELETED,0)  from shop where id  = ?", Integer.class, shopId);
            if(isDeleted== Constants.ONE){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已删除,请联系管理员");
            }
            Integer isForbidden = dao.queryForObject(" select COALESCE(STATUS,0)  from shop where id  = ?", Integer.class, shopId);
            if(isForbidden== Constants.ONE){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已禁用,请联系管理员");
            }
            Integer count = dao.queryForObject("select count(1) from shop where id  = ?", Integer.class, shopId);
            if (count != null && count > 0) {
                request.setAttribute(JwtTokenUtil.ShopId_Name, shopId);
                return true;
            }else{
                throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"用户信息出错");
            }
        } catch (IllegalArgumentException | JwtException e) {
            throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录");
        }
    }
 
 
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
 
 
}