Search in sources :

Example 1 with User

use of com.gmoon.springsecurityjwt.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 = request.getHeader(JwtAuthenticationFilter.HEADER_NAME);
    try {
        User user = jwtUtil.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.springsecurityjwt.jwt.exception.JwtVerifyException) User(com.gmoon.springsecurityjwt.user.User) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 2 with User

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

the class JwtUtilTest method testDecode.

@Test
@DisplayName("JWT 복호화 검증")
void testDecode() {
    // when
    String token = jwtUtil.generate(User.create("gmoon", "123", Role.ADMIN));
    User user = jwtUtil.decode(token);
    // then
    assertThat(user).hasFieldOrPropertyWithValue("username", "gmoon").hasFieldOrPropertyWithValue("role", Role.ADMIN);
}
Also used : User(com.gmoon.springsecurityjwt.user.User) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with User

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

the class TeamControllerTest method testDelete.

@Test
@DisplayName("팀을 삭제한다.")
void testDelete() throws Exception {
    // given
    User admin = getUserOrElseThrow("admin");
    // when
    ResultActions result = verify(delete(URL_OF_TEAM), admin);
    // then
    result.andExpect(status().isOk());
}
Also used : User(com.gmoon.springsecurityjwt.user.User) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.jupiter.api.Test) BaseSpringBootTest(com.gmoon.springsecurityjwt.base.BaseSpringBootTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with User

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

the class TeamControllerTest method testGet.

@Test
@DisplayName("팀을 조회한다.")
void testGet() throws Exception {
    // given
    User admin = getUserOrElseThrow("admin");
    // when
    ResultActions result = verify(get(URL_OF_TEAM), admin);
    // then
    result.andExpect(status().isOk());
    result.andExpect(jsonPath("$.id").value(WEB_TEAM_ID));
}
Also used : User(com.gmoon.springsecurityjwt.user.User) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.jupiter.api.Test) BaseSpringBootTest(com.gmoon.springsecurityjwt.base.BaseSpringBootTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with User

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

the class JacksonUtilsTest method testToString.

@Test
void testToString() {
    // given
    User user = User.create("gmoon", "123", Role.ADMIN);
    // when
    String jsonString = JacksonUtils.toString(user);
    // then
    assertThat(jsonString).contains("username", "gmoon", "authorities", "ADMIN");
}
Also used : User(com.gmoon.springsecurityjwt.user.User) Test(org.junit.jupiter.api.Test) JsonTest(org.springframework.boot.test.autoconfigure.json.JsonTest)

Aggregations

User (com.gmoon.springsecurityjwt.user.User)18 Test (org.junit.jupiter.api.Test)14 DisplayName (org.junit.jupiter.api.DisplayName)8 BaseSpringBootTest (com.gmoon.springsecurityjwt.base.BaseSpringBootTest)6 JsonTest (org.springframework.boot.test.autoconfigure.json.JsonTest)6 ResultActions (org.springframework.test.web.servlet.ResultActions)6 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)2 JwtVerifyException (com.gmoon.springsecurityjwt.jwt.exception.JwtVerifyException)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2