Search in sources :

Example 1 with AuthenticationImpl

use of run.halo.app.security.authentication.AuthenticationImpl in project halo by halo-dev.

the class SensitiveConcealAspectTest method testAdmin.

@Test
void testAdmin() {
    SecurityContextHolder.setContext(new SecurityContextImpl(new AuthenticationImpl(new UserDetail(new User()))));
    List<PostComment> postComments = postCommentService.listBy(1);
    for (PostComment postComment : postComments) {
        assertEquals("127.0.0.1", postComment.getIpAddress());
        assertEquals("hi@halo.run", postComment.getEmail());
    }
}
Also used : AuthenticationImpl(run.halo.app.security.authentication.AuthenticationImpl) UserDetail(run.halo.app.security.support.UserDetail) SecurityContextImpl(run.halo.app.security.context.SecurityContextImpl) User(run.halo.app.model.entity.User) PostComment(run.halo.app.model.entity.PostComment) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with AuthenticationImpl

use of run.halo.app.security.authentication.AuthenticationImpl in project halo-plugin-experimental by guqing.

the class AdminAuthenticationFilter method doAuthenticate.

@Override
protected void doAuthenticate(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    if (!haloProperties.isAuthEnabled()) {
        // Set security
        userService.getCurrentUser().ifPresent(user -> SecurityContextHolder.setContext(new SecurityContextImpl(new AuthenticationImpl(new UserDetail(user)))));
        // Do filter
        filterChain.doFilter(request, response);
        return;
    }
    // Get token from request
    String token = getTokenFromRequest(request);
    if (StringUtils.isBlank(token)) {
        throw new AuthenticationException("未登录,请登录后访问");
    }
    // Get user id from cache
    Optional<Integer> optionalUserId = cacheStore.getAny(SecurityUtils.buildTokenAccessKey(token), Integer.class);
    if (!optionalUserId.isPresent()) {
        throw new AuthenticationException("Token 已过期或不存在").setErrorData(token);
    }
    // Get the user
    User user = userService.getById(optionalUserId.get());
    // Build user detail
    UserDetail userDetail = new UserDetail(user);
    // Set security
    SecurityContextHolder.setContext(new SecurityContextImpl(new AuthenticationImpl(userDetail)));
    // Do filter
    filterChain.doFilter(request, response);
}
Also used : AuthenticationImpl(run.halo.app.security.authentication.AuthenticationImpl) UserDetail(run.halo.app.security.support.UserDetail) SecurityContextImpl(run.halo.app.security.context.SecurityContextImpl) User(run.halo.app.model.entity.User) AuthenticationException(run.halo.app.exception.AuthenticationException)

Example 3 with AuthenticationImpl

use of run.halo.app.security.authentication.AuthenticationImpl in project halo by halo-dev.

the class AdminAuthenticationFilter method doAuthenticate.

@Override
protected void doAuthenticate(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    if (!haloProperties.isAuthEnabled()) {
        // Set security
        userService.getCurrentUser().ifPresent(user -> SecurityContextHolder.setContext(new SecurityContextImpl(new AuthenticationImpl(new UserDetail(user)))));
        // Do filter
        filterChain.doFilter(request, response);
        return;
    }
    // Get token from request
    String token = getTokenFromRequest(request);
    if (StringUtils.isBlank(token)) {
        throw new AuthenticationException("未登录,请登录后访问");
    }
    // Get user id from cache
    Optional<Integer> optionalUserId = cacheStore.getAny(SecurityUtils.buildTokenAccessKey(token), Integer.class);
    if (!optionalUserId.isPresent()) {
        throw new AuthenticationException("Token 已过期或不存在").setErrorData(token);
    }
    // Get the user
    User user = userService.getById(optionalUserId.get());
    // Build user detail
    UserDetail userDetail = new UserDetail(user);
    // Set security
    SecurityContextHolder.setContext(new SecurityContextImpl(new AuthenticationImpl(userDetail)));
    // Do filter
    filterChain.doFilter(request, response);
}
Also used : AuthenticationImpl(run.halo.app.security.authentication.AuthenticationImpl) UserDetail(run.halo.app.security.support.UserDetail) SecurityContextImpl(run.halo.app.security.context.SecurityContextImpl) User(run.halo.app.model.entity.User) AuthenticationException(run.halo.app.exception.AuthenticationException)

Example 4 with AuthenticationImpl

use of run.halo.app.security.authentication.AuthenticationImpl in project halo by ruibaby.

the class SensitiveConcealAspectTest method testAdmin.

@Test
void testAdmin() {
    SecurityContextHolder.setContext(new SecurityContextImpl(new AuthenticationImpl(new UserDetail(new User()))));
    List<PostComment> postComments = postCommentService.listBy(1);
    for (PostComment postComment : postComments) {
        assertEquals("127.0.0.1", postComment.getIpAddress());
        assertEquals("hi@halo.run", postComment.getEmail());
    }
}
Also used : AuthenticationImpl(run.halo.app.security.authentication.AuthenticationImpl) UserDetail(run.halo.app.security.support.UserDetail) SecurityContextImpl(run.halo.app.security.context.SecurityContextImpl) User(run.halo.app.model.entity.User) PostComment(run.halo.app.model.entity.PostComment) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with AuthenticationImpl

use of run.halo.app.security.authentication.AuthenticationImpl in project halo by ruibaby.

the class AdminAuthenticationFilter method doAuthenticate.

@Override
protected void doAuthenticate(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    if (!haloProperties.isAuthEnabled()) {
        // Set security
        userService.getCurrentUser().ifPresent(user -> SecurityContextHolder.setContext(new SecurityContextImpl(new AuthenticationImpl(new UserDetail(user)))));
        // Do filter
        filterChain.doFilter(request, response);
        return;
    }
    // Get token from request
    String token = getTokenFromRequest(request);
    if (StringUtils.isBlank(token)) {
        throw new AuthenticationException("未登录,请登录后访问");
    }
    // Get user id from cache
    Optional<Integer> optionalUserId = cacheStore.getAny(SecurityUtils.buildTokenAccessKey(token), Integer.class);
    if (!optionalUserId.isPresent()) {
        throw new AuthenticationException("Token 已过期或不存在").setErrorData(token);
    }
    // Get the user
    User user = userService.getById(optionalUserId.get());
    // Build user detail
    UserDetail userDetail = new UserDetail(user);
    // Set security
    SecurityContextHolder.setContext(new SecurityContextImpl(new AuthenticationImpl(userDetail)));
    // Do filter
    filterChain.doFilter(request, response);
}
Also used : AuthenticationImpl(run.halo.app.security.authentication.AuthenticationImpl) UserDetail(run.halo.app.security.support.UserDetail) SecurityContextImpl(run.halo.app.security.context.SecurityContextImpl) User(run.halo.app.model.entity.User) AuthenticationException(run.halo.app.exception.AuthenticationException)

Aggregations

User (run.halo.app.model.entity.User)5 AuthenticationImpl (run.halo.app.security.authentication.AuthenticationImpl)5 SecurityContextImpl (run.halo.app.security.context.SecurityContextImpl)5 UserDetail (run.halo.app.security.support.UserDetail)5 AuthenticationException (run.halo.app.exception.AuthenticationException)3 Test (org.junit.jupiter.api.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 PostComment (run.halo.app.model.entity.PostComment)2