Search in sources :

Example 11 with User

use of com.gmoon.springsecurityjwt.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 = jwtUtil.generate(user);
    response.setHeader(HEADER_NAME, token);
}
Also used : User(com.gmoon.springsecurityjwt.user.User)

Example 12 with User

use of com.gmoon.springsecurityjwt.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 = jwtUtil.generate(user);
    response.setHeader(HEADER_NAME, token);
}
Also used : User(com.gmoon.springsecurityjwt.user.User)

Example 13 with User

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

the class TeamControllerTest method testDelete_forbidden.

@Test
@DisplayName("팀 삭제는 어드민 계정만 가능하다. " + "권한이 없는 계정이 접근할 경우 Forbidden(403) 에러가 발생한다.")
void testDelete_forbidden() throws Exception {
    // given
    User user = getUserOrElseThrow("user1");
    // when
    ResultActions result = verify(delete(URL_OF_TEAM), user);
    // then
    result.andExpect(status().is4xxClientError());
    result.andExpect(status().isForbidden());
}
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 14 with User

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

the class JacksonUtilsTest method testToObject.

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

Example 15 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)

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