Compare commits
No commits in common. "fd0cb2d3453e84204e7a34c8c90b4bbe0c98c87b" and "3c2e353a60d5c837ffda4c54740b05aa4eabc0d4" have entirely different histories.
fd0cb2d345
...
3c2e353a60
@ -1,15 +0,0 @@
|
|||||||
package org.cmh.backend.Utils;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
||||||
|
|
||||||
@ControllerAdvice
|
|
||||||
public class GlobalExceptionHandler {
|
|
||||||
|
|
||||||
@ExceptionHandler(JwtValidationException.class)
|
|
||||||
public ResponseEntity<Object> handleJwtInvalidException(JwtValidationException ex) {
|
|
||||||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,12 +0,0 @@
|
|||||||
package org.cmh.backend.Utils;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
public class JwtRequest {
|
|
||||||
private String token;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
package org.cmh.backend.Utils;
|
|
||||||
|
|
||||||
public class JwtValidationException extends RuntimeException {
|
|
||||||
public JwtValidationException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,12 +0,0 @@
|
|||||||
package org.cmh.backend.Utils;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
@Target(ElementType.METHOD)
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
public @interface JwtVerify {
|
|
||||||
}
|
|
||||||
|
|
||||||
@ -1,20 +0,0 @@
|
|||||||
package org.cmh.backend.Utils;
|
|
||||||
|
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
|
||||||
import org.aspectj.lang.annotation.Before;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Aspect
|
|
||||||
@Component
|
|
||||||
public class JwtVerifyAspect {
|
|
||||||
@Before("@annotation(JwtVerify) && args(request,..)")
|
|
||||||
public void verifyJwtToken(Object request) throws JwtValidationException {
|
|
||||||
if (request instanceof JwtRequest) {
|
|
||||||
String token = ((JwtRequest) request).getToken();
|
|
||||||
if (!JwtUtil.isTokenValid(token)) {
|
|
||||||
throw new JwtValidationException("JWT token is invalid");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ -1,8 +1,6 @@
|
|||||||
package org.cmh.backend.Utils;
|
package org.cmh.backend.Utils;
|
||||||
|
|
||||||
import io.jsonwebtoken.Claims;
|
import io.jsonwebtoken.Claims;
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -55,49 +53,5 @@ public class JwtUtilTest {
|
|||||||
Assert.assertFalse("Invalid token should not be valid", JwtUtil.isTokenValid(invalidToken, "validUser"));
|
Assert.assertFalse("Invalid token should not be valid", JwtUtil.isTokenValid(invalidToken, "validUser"));
|
||||||
Assert.assertTrue("Valid token should be valid", JwtUtil.isTokenValid(validToken, "validUser"));
|
Assert.assertTrue("Valid token should be valid", JwtUtil.isTokenValid(validToken, "validUser"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
private class SomeJwtRequest extends JwtRequest {
|
|
||||||
String msg;
|
|
||||||
|
|
||||||
public SomeJwtRequest(String token, String msg) {
|
|
||||||
super.setToken(token);
|
|
||||||
this.msg = msg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class SomeController {
|
|
||||||
private final SomeJwtRequest request;
|
|
||||||
|
|
||||||
SomeController(String token) {
|
|
||||||
this.request = new SomeJwtRequest(token, "test");
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean run() {
|
|
||||||
try {
|
|
||||||
return verify(request);
|
|
||||||
} catch (JwtValidationException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@JwtVerify
|
|
||||||
public boolean verify(SomeJwtRequest request) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testVerify() {
|
|
||||||
//TODO:这里似乎不能这样测试,待修改或忽略
|
|
||||||
String username = "testUser";
|
|
||||||
String token = JwtUtil.generateToken(username);
|
|
||||||
SomeController validTokenController = new SomeController(token);
|
|
||||||
SomeController invalidTokenController = new SomeController("invalidToken");
|
|
||||||
Assert.assertFalse("Valid token should pass verification", validTokenController.run());
|
|
||||||
Assert.assertFalse("Invalid token should fail verification", invalidTokenController.run());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,88 +0,0 @@
|
|||||||
package org.cmh.backend.Utils;
|
|
||||||
|
|
||||||
import org.cmh.backend.authentication.service.UserService;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.InjectMocks;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public class JwtVerifyAspectTest {
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableAspectJAutoProxy
|
|
||||||
@Import({JwtVerifyAspect.class})
|
|
||||||
static class Config {
|
|
||||||
@Bean
|
|
||||||
public JwtUtil jwtUtil() {
|
|
||||||
return Mockito.mock(JwtUtil.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public UserService userService() {
|
|
||||||
return Mockito.mock(UserService.class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private JwtUtil jwtUtil = new JwtUtil();
|
|
||||||
|
|
||||||
@InjectMocks
|
|
||||||
private JwtVerifyAspect jwtVerifyAspect;
|
|
||||||
|
|
||||||
@BeforeClass
|
|
||||||
public static void setUpClass() {
|
|
||||||
// Static setup if needed
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
Mockito.when(jwtUtil.isTokenValid("validToken")).thenReturn(true);
|
|
||||||
Mockito.when(jwtUtil.isTokenValid("invalidToken")).thenReturn(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO:这个测试跑不动,有问题,先取消掉
|
|
||||||
// @Test
|
|
||||||
// public void testVerify() {
|
|
||||||
// SomeController validTokenController = new SomeController("validToken");
|
|
||||||
// SomeController invalidTokenController = new SomeController("invalidToken");
|
|
||||||
//
|
|
||||||
// Assert.assertTrue("Valid token should pass verification", validTokenController.run());
|
|
||||||
// Assert.assertFalse("Invalid token should fail verification", invalidTokenController.run());
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
class SomeController {
|
|
||||||
private SomeJwtRequest request;
|
|
||||||
|
|
||||||
SomeController(String token) {
|
|
||||||
this.request = new SomeJwtRequest(token, "test");
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean run() {
|
|
||||||
try {
|
|
||||||
return verify(request);
|
|
||||||
} catch (JwtValidationException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@JwtVerify
|
|
||||||
public boolean verify(SomeJwtRequest request) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SomeJwtRequest extends JwtRequest {
|
|
||||||
String msg;
|
|
||||||
|
|
||||||
public SomeJwtRequest(String token, String msg) {
|
|
||||||
super.setToken(token);
|
|
||||||
this.msg = msg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user