Skip to content

Commit

Permalink
✅ add refresh tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sanshengshui committed Nov 3, 2020
1 parent 5df184d commit 0528eb8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class CustomJwtAuthenticationFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {

try{
// JWT Token is in the form "Bearer token". Remove Bearer word and
// get only the Token
Expand All @@ -46,7 +47,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
// Spring Security Configurations successfully.
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
} else {
System.out.println("Cannot set the Security Context");
logger.info("Cannot set the Security Context");
}
}catch(ExpiredJwtException ex)
{
Expand All @@ -55,8 +56,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
// allow for Refresh Token creation if following conditions are true.
if (isRefreshToken != null && isRefreshToken.equals("true") && requestURL.contains("refreshtoken")) {
allowForRefreshToken(ex, request);
} else
} else {
request.setAttribute("exception", ex);
}
}
catch(BadCredentialsException ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package iot.technology.jwt.refresh.config;

import io.jsonwebtoken.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
Expand All @@ -15,6 +16,7 @@
* @date 2020/9/29 15:59
*/
@Service
@Slf4j
public class JwtUtil {

private String secret;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
### 1. 注册一个用户名和密码
POST http://localhost:8080/register
Content-Type: application/json

{
"username":"iot.technology",
"password":"make-great",
"role": "ROLE_ADMIN"
}

### 2. 获取JWT
POST http://localhost:8080/authenticate
Content-Type: application/json

{
"username":"iot.technology",
"password":"make-great"
}

### 3. 请求/hello路径地址获取数据
GET http://localhost:8080/hellouser
Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJpb3QudGVjaG5vbG9neSIsImlzQWRtaW4iOnRydWUsImV4cCI6MTYwNDM5ODg4MCwiaWF0IjoxNjA0Mzk4ODgwfQ.mQDYYkk0jncA0PNwO7T_0nRUKt6p7T6ULwLLuRbsEGfjroXIw1S3AyUmI8xF7Ze_p_4YVCGD6WAMVzr42Ig7tA
###

### 3. 请求/hello路径地址获取数据
GET http://localhost:8080/refreshtoken
Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJpb3QudGVjaG5vbG9neSIsImlzQWRtaW4iOnRydWUsImV4cCI6MTYwNDQwNzIxOSwiaWF0IjoxNjA0Mzk4MjE5fQ.fyad7dExh8dMpYVCkmKRnFFZDsHV1o8kLlRcy1aXSETvrcoi_YzWwwL8lCs-JA1kPoMHle3Bz12c_rzhoZwvEQ
###

0 comments on commit 0528eb8

Please sign in to comment.