【问题标题】:How to set Manually Authenticate User with Spring boot Security如何使用 Spring Boot 安全性设置手动身份验证用户
【发布时间】:2018-11-09 09:49:25
【问题描述】:

我使用 SSO 登录创建了 Spring Boot 应用程序。我已经为此使用了 saml.xml 文件。 SSO 登录后,我调用了getAuthentication() 方法,它将返回annonymousUser 每次。我想获取 SSO 记录的用户详细信息。

Principal principal = 
        SecurityContextHolder.getContext().getAuthentication();

安全配置类如下所示:

@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/actuator").authenticated();
    http.headers().cacheControl().disable();
    http.csrf().disable();
    http.logout().logoutSuccessUrl("/assets/logout.html");
}
}

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    您可以像下面这样让用户登录到 Spring Security。

    public void login(HttpServletRequest req, String user, String pass) { 
        UsernamePasswordAuthenticationToken authReq
          = new UsernamePasswordAuthenticationToken(user, pass);
        Authentication auth = authManager.authenticate(authReq);
    
        SecurityContext sc = SecurityContextHolder.getContext();
        sc.setAuthentication(auth);
        HttpSession session = req.getSession(true);
        session.setAttribute(SPRING_SECURITY_CONTEXT_KEY, sc);
    }
    

    参考manually-set-user-authentication-spring-security

    【讨论】:

    • 如何获取用户名和密码?我用过sso。
    • @bhagyaperera 创建一个实现Authentication 的自定义类,而不是使用UsernamePasswordAuthenticationToken 然后
    猜你喜欢
    • 2014-07-06
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    相关资源
    最近更新 更多