Spring Security登录 成功后用于信息保存

在上片博客中 记录了登录流程:那登录后信息如果用户信息保存到哪里了呢?
看最后源码:
在抽象类 AbstractAuthenticationProcessingFilter 拦截并做登录处理后 最后会调用

successfulAuthentication(request, response, chain, authResult);
1
方法 successfulAuthentication 中可以明确看到 信息保存到了SecurityContextHolder.getContext().setAuthentication(authResult);

所以获取的时候 可以

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

在这里插入图片描述

在这里插入图片描述

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Security是一个基于Spring框架的安全框架,可以帮助我们实现认证和授权等安全功能。在Spring Security中,我们可以通过自定义登录页面来实现自定义登录。 具体实现步骤如下: 1. 创建一个登录页面,可以使用JSP、Thymeleaf等模板引擎来实现。 2. 在Spring Security配置中,指定自定义登录页面的URL和处理登录请求的URL。 3. 创建一个自定义的AuthenticationProvider,用于验证用户的登录信息。 4. 在自定义的AuthenticationProvider中,实现用户信息的验证逻辑,包括用户名、密码等信息的验证。 5. 在验证通过后,将用户信息封装成一个Authentication对象,并返回给Spring Security。 6. 在Spring Security中,将Authentication对象保存SecurityContextHolder中,以便后续的授权操作。 通过以上步骤,我们就可以实现自定义登录功能。需要注意的是,在实现自定义登录时,我们需要遵循Spring Security的安全规范,确保系统的安全性和稳定性。 ### 回答2: Spring Security是一个基于Spring框架的安全框架,常用于web应用程序的安全控制。而自定义登录Spring Security的一个很重要的功能之一,可以让用户通过更加灵活和自由的方式登录系统。下面我将详细阐述Spring Security自定义登录的相关知识点。 1.自定义登录页面 我们可以通过自定义登录页面的形式来实现登录页面视觉效果的自定义。通常情况下,我们需要在配置文件中引入一个login-page标签,然后将其指向我们自己创建的html页面。 2.自定义验证器 Spring Security默认的验证器是AuthenticationProvider,但是我们也可以自定义自己的验证器,从而可以实现对用户提交的用户名和密码的验证。自定义的验证器通常需要实现AuthenticationProvider接口,并重写authenticate方法和supports方法。 3.自定义安全配置 我们可以通过自定义安全配置来强化系统的安全性。自定义安全配置通常需要继承WebSecurityConfigurerAdapter,并重写configure方法,从而可以实现自定义的安全配置。我们可以在这自定义授权、认证、登录、异常处理等相关内容。 4.自定义认证成功和失败处理器 系统认证成功或失败后需要做一些后续处理,我们可以通过自定义认证成功和失败处理器来实现相应的功能。自定义认证成功和失败处理器通常需要实现AuthenticationSuccessHandler和AuthenticationFailureHandler接口。 5.自定义记住我功能 记住我功能可以实现用户重新登录时,不需要输入用户名和密码等信息。我们可以通过配置remember-me元素实现此功能。而实现自定义的记住我功能通常需要实现RememberMeServices接口,并在remember-me元素中使用custom-service-ref配置项指定实现类。 在实际的开发过程中,Spring Security自定义登录是非常常见的需求。只有通过灵活的自定义,我们才能够让系统更好地适应不同的业务需求。以上便是我对Spring Security自定义登录的详细阐述。 ### 回答3: Spring Security 是一款广泛被使用的安全框架,也是 Spring 框架的一部分,十分方便地实现了众多的安全机制。在默认配置下,Spring Security 提供了许多登录验证的实现,例如表单登录和基本认证,但仍有许多场景需要开发人员进行自定义实现。本文将着重介绍如何进行 Spring Security 自定义登录的实现。 一、配置类 首先,需要在配置类中进行一些 Bean 的配置,例如 UserDetailsService 和 PasswordEncoder,这些都是在认证过程中必要的配置项。 @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } } 二、自定义登录页 在默认配置下,Spring Security 提供的登录页十分简陋,我们需要自定义一个更为合适的登录界面。实现方式有很多,通常可使用 Thymeleaf 模板引擎来渲染登录页。 @Configuration public class MvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); } } 三、认证过程 自定义认证过程的实现,通常需要扩展 AbstractAuthenticationProcessingFilter 这个过滤器。我们需要为其提供一个 AuthenticationManager,以此来进行认证流程的管理。 public class CustomAuthenticationFilter extends AbstractAuthenticationProcessingFilter { public CustomAuthenticationFilter() { super(new AntPathRequestMatcher("/login", "POST")); } @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { if (!request.getMethod().equals("POST")) { throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); } BufferedReader br = request.getReader(); String line; String requestBody = ""; while ((line = br.readLine()) != null) { requestBody += line; } ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(requestBody); String username = jsonNode.get("username").asText(); String password = jsonNode.get("password").asText(); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); return this.getAuthenticationManager().authenticate(token); } } 四、自定义用户信息 如果需要自定义用户信息,我们需要实现 UserDetails 接口和 UserDetailsService 接口。UserDetails 接口存储用户的基本信息,例如用户名,密码和授权信息。UserDetailsService 接口负责从持久化层读取用户信息。 @Service public class CustomUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 从持久化层读取用户信息 return new User(username, "password", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER")); } } 五、总结 本文简单介绍了 Spring Security 自定义登录的实现过程,需要开发人员在配置类,自定义登录页,认证过程以及自定义用户信息上下功夫。自定义登录能够解决默认实现无法满足的需求,提高系统的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值