Skip to content

Commit 15bfd33

Browse files
author
feiweiwei
committed
v0.5 6. 安全-jwt 通过interceptor处理@Login注解,验证token
1 parent d584b96 commit 15bfd33

15 files changed

Lines changed: 407 additions & 14 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.monkey01.common.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* app登录效验
7+
* @author feiweiwei
8+
9+
* @date 2017/9/23 14:30
10+
*/
11+
@Target(ElementType.METHOD)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Documented
14+
public @interface Login {
15+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.monkey01.common.annotation.interceptor;
2+
3+
4+
import com.monkey01.common.annotation.Login;
5+
import com.monkey01.common.exception.MkException;
6+
import com.monkey01.common.utils.JwtUtils;
7+
import io.jsonwebtoken.Claims;
8+
9+
import org.apache.commons.lang.StringUtils;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.http.HttpStatus;
12+
import org.springframework.stereotype.Component;
13+
import org.springframework.web.method.HandlerMethod;
14+
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
15+
16+
import javax.servlet.http.HttpServletRequest;
17+
import javax.servlet.http.HttpServletResponse;
18+
19+
/**
20+
* 权限(Token)jwt切面验证
21+
* @author feiweiwei
22+
23+
* @date 2017-01-20 19:23
24+
*/
25+
@Component
26+
public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
27+
@Autowired
28+
private JwtUtils jwtUtils;
29+
30+
public static final String USER_KEY = "userId";
31+
32+
@Override
33+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
34+
Login annotation;
35+
if(handler instanceof HandlerMethod) {
36+
annotation = ((HandlerMethod) handler).getMethodAnnotation(Login.class);
37+
}else{
38+
return true;
39+
}
40+
41+
if(annotation == null){
42+
return true;
43+
}
44+
45+
//获取用户凭证
46+
String token = request.getHeader(jwtUtils.getHeader());
47+
if(StringUtils.isBlank(token)){
48+
token = request.getParameter(jwtUtils.getHeader());
49+
}
50+
51+
//凭证为空
52+
if(StringUtils.isBlank(token)){
53+
throw new MkException(jwtUtils.getHeader() + "不能为空", HttpStatus.UNAUTHORIZED.toString());
54+
}
55+
56+
Claims claims = jwtUtils.getClaimByToken(token);
57+
if(claims == null || jwtUtils.isTokenExpired(claims.getExpiration())){
58+
throw new MkException(jwtUtils.getHeader() + "失效,请重新登录", HttpStatus.UNAUTHORIZED.toString());
59+
}
60+
61+
//设置userId到request里,后续根据userId,获取用户信息
62+
request.setAttribute(USER_KEY, Long.parseLong(claims.getSubject()));
63+
64+
return true;
65+
}
66+
}

src/main/java/com/monkey01/common/config/WebMvcConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.monkey01.common.config;
22

3+
import com.monkey01.common.annotation.interceptor.AuthorizationInterceptor;
34
import com.monkey01.common.annotation.resolver.DesParamsHandlerMethodArgumentResolver;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.context.annotation.Configuration;
67
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
8+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
79
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
810

911
import java.util.List;
@@ -20,8 +22,13 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter {
2022

2123
@Autowired
2224
private DesParamsHandlerMethodArgumentResolver desParamsHandlerMethodArgumentResolver;
25+
@Autowired
26+
private AuthorizationInterceptor authorizationInterceptor;
2327

24-
28+
@Override
29+
public void addInterceptors(InterceptorRegistry registry) {
30+
registry.addInterceptor(authorizationInterceptor).addPathPatterns("/**");
31+
}
2532
@Override
2633
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
2734
argumentResolvers.add(desParamsHandlerMethodArgumentResolver);

src/main/java/com/monkey01/modules/controller/TestController.java renamed to src/main/java/com/monkey01/modules/module1/controller/TestController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.monkey01.modules.controller;
1+
package com.monkey01.modules.module1.controller;
22

33
import com.monkey01.common.annotation.MkParams;
44
import com.monkey01.common.annotation.SysLog;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.monkey01.modules.module1.controller;
2+
3+
4+
import com.monkey01.common.annotation.Login;
5+
import com.monkey01.common.annotation.interceptor.AuthorizationInterceptor;
6+
import com.monkey01.common.domain.MkResponse;
7+
import com.monkey01.common.utils.JwtUtils;
8+
import com.monkey01.modules.module1.domain.dto.UserDO;
9+
import com.monkey01.modules.module1.domain.vo.LoginVO;
10+
import com.monkey01.modules.module1.domain.vo.RegisterVO;
11+
import com.monkey01.modules.module1.service.UserService;
12+
import org.apache.commons.codec.digest.DigestUtils;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.RequestBody;
16+
import org.springframework.web.bind.annotation.RestController;
17+
18+
import javax.servlet.http.HttpServletRequest;
19+
import java.util.Date;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
24+
@RestController
25+
public class TestUserController {
26+
@Autowired
27+
private UserService userService;
28+
@Autowired
29+
private JwtUtils jwtUtils;
30+
31+
/**
32+
* 登录
33+
*/
34+
@PostMapping("login")
35+
public MkResponse login(@RequestBody LoginVO form){
36+
37+
//用户登录
38+
long userId = userService.login(form);
39+
40+
//生成token
41+
String token = jwtUtils.generateToken(userId);
42+
43+
Map<String, Object> map = new HashMap<>();
44+
map.put("token", token);
45+
map.put("expire", jwtUtils.getExpire());
46+
47+
return MkResponse.ok(map);
48+
}
49+
50+
@PostMapping("register")
51+
public MkResponse register(@RequestBody RegisterVO form){
52+
UserDO user = new UserDO();
53+
user.setMobile(form.getMobile());
54+
user.setUsername(form.getMobile());
55+
user.setPassword(DigestUtils.sha256Hex(form.getPassword()));
56+
user.setCreateTime(new Date());
57+
userService.save(user);
58+
59+
return MkResponse.ok();
60+
}
61+
62+
@Login
63+
@PostMapping("queryUser")
64+
public MkResponse queryUser(HttpServletRequest request){
65+
Long userId = (Long)request.getAttribute(AuthorizationInterceptor.USER_KEY);
66+
67+
UserDO userDO = userService.getById(userId);
68+
return MkResponse.ok().put("user", userDO);
69+
}
70+
71+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2018 人人开源 http://www.renren.io
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.monkey01.modules.module1.dao.mapper;
18+
19+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
20+
import com.monkey01.modules.module1.domain.dto.UserDO;
21+
import org.apache.ibatis.annotations.Mapper;
22+
23+
24+
@Mapper
25+
public interface UserMapper extends BaseMapper<UserDO> {
26+
27+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.monkey01.modules.module1.domain.dto;
2+
3+
import com.baomidou.mybatisplus.annotation.TableId;
4+
import com.baomidou.mybatisplus.annotation.TableName;
5+
6+
import java.util.Date;
7+
8+
/**
9+
* @author: feiweiwei
10+
* @description:
11+
* @created Date: 10:11 18/9/6.
12+
* @modify by:
13+
*/
14+
@TableName("test_user")
15+
public class UserDO {
16+
private static final long serialVersionUID = 1L;
17+
18+
@TableId
19+
private Long userId;
20+
private String username;
21+
private String mobile;
22+
private String password;
23+
private Date createTime;
24+
25+
public static long getSerialVersionUID() {
26+
return serialVersionUID;
27+
}
28+
29+
public Long getUserId() {
30+
return userId;
31+
}
32+
33+
public void setUserId(Long userId) {
34+
this.userId = userId;
35+
}
36+
37+
public String getUsername() {
38+
return username;
39+
}
40+
41+
public void setUsername(String username) {
42+
this.username = username;
43+
}
44+
45+
public String getMobile() {
46+
return mobile;
47+
}
48+
49+
public void setMobile(String mobile) {
50+
this.mobile = mobile;
51+
}
52+
53+
public String getPassword() {
54+
return password;
55+
}
56+
57+
public void setPassword(String password) {
58+
this.password = password;
59+
}
60+
61+
public Date getCreateTime() {
62+
return createTime;
63+
}
64+
65+
public void setCreateTime(Date createTime) {
66+
this.createTime = createTime;
67+
}
68+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright 2018 人人开源 http://www.renren.io
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.monkey01.modules.module1.domain.vo;
18+
19+
20+
import javax.validation.constraints.NotBlank;
21+
22+
23+
public class LoginVO {
24+
@NotBlank(message="手机号不能为空")
25+
private String mobile;
26+
27+
@NotBlank(message="密码不能为空")
28+
private String password;
29+
30+
public String getMobile() {
31+
return mobile;
32+
}
33+
34+
public void setMobile(String mobile) {
35+
this.mobile = mobile;
36+
}
37+
38+
public String getPassword() {
39+
return password;
40+
}
41+
42+
public void setPassword(String password) {
43+
this.password = password;
44+
}
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2018 人人开源 http://www.renren.io
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.monkey01.modules.module1.domain.vo;
18+
19+
20+
21+
public class RegisterVO {
22+
23+
private String mobile;
24+
private String password;
25+
26+
public String getMobile() {
27+
return mobile;
28+
}
29+
30+
public void setMobile(String mobile) {
31+
this.mobile = mobile;
32+
}
33+
34+
public String getPassword() {
35+
return password;
36+
}
37+
38+
public void setPassword(String password) {
39+
this.password = password;
40+
}
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.monkey01.modules.module1.service;
2+
3+
4+
import com.baomidou.mybatisplus.extension.service.IService;
5+
import com.monkey01.modules.module1.domain.dto.UserDO;
6+
import com.monkey01.modules.module1.domain.vo.LoginVO;
7+
8+
9+
public interface UserService extends IService<UserDO> {
10+
11+
UserDO queryByMobile(String mobile);
12+
13+
long login(LoginVO loginVO);
14+
}

0 commit comments

Comments
 (0)