Search in sources :

Example 1 with User

use of com.gmoon.resourceserver.user.User in project toy by gmoon92.

the class JwtVerifyFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
    String token = getToken(request);
    try {
        User user = jwtUtils.decode(token);
        Authentication authentication = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authentication);
        chain.doFilter(request, response);
    } catch (JWTVerificationException e) {
        SecurityContextHolder.clearContext();
        getAuthenticationEntryPoint().commence(request, response, new JwtVerifyException(e));
    }
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) JwtVerifyException(com.gmoon.resourceserver.jwt.exception.JwtVerifyException) User(com.gmoon.resourceserver.user.User) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 2 with User

use of com.gmoon.resourceserver.user.User in project Toy by gmoon92.

the class JwtAuthenticationFilter method successfulAuthentication.

@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication) throws IOException, ServletException {
    User user = (User) authentication.getPrincipal();
    String token = jwtUtils.generate(user);
    response.setHeader(HttpHeaders.AUTHORIZATION, token);
}
Also used : User(com.gmoon.resourceserver.user.User)

Example 3 with User

use of com.gmoon.resourceserver.user.User in project Toy by gmoon92.

the class JwtVerifyFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
    String token = getToken(request);
    try {
        User user = jwtUtils.decode(token);
        Authentication authentication = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authentication);
        chain.doFilter(request, response);
    } catch (JWTVerificationException e) {
        SecurityContextHolder.clearContext();
        getAuthenticationEntryPoint().commence(request, response, new JwtVerifyException(e));
    }
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) JwtVerifyException(com.gmoon.resourceserver.jwt.exception.JwtVerifyException) User(com.gmoon.resourceserver.user.User) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 4 with User

use of com.gmoon.resourceserver.user.User in project toy by gmoon92.

the class JwtAuthenticationFilter method successfulAuthentication.

@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication) throws IOException, ServletException {
    User user = (User) authentication.getPrincipal();
    String token = jwtUtils.generate(user);
    response.setHeader(HttpHeaders.AUTHORIZATION, token);
}
Also used : User(com.gmoon.resourceserver.user.User)

Example 5 with User

use of com.gmoon.resourceserver.user.User in project toy by gmoon92.

the class BaseIntegrationTest method beforeAll.

@BeforeAll
static void beforeAll(@Autowired UserRepository repository, @Autowired JwtUtils jwtUtils) {
    User admin = repository.findByUsername("admin");
    TOKEN_OF_ADMIN = new Token(jwtUtils.generate(admin));
}
Also used : User(com.gmoon.resourceserver.user.User) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

User (com.gmoon.resourceserver.user.User)6 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)2 JwtVerifyException (com.gmoon.resourceserver.jwt.exception.JwtVerifyException)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2