Spring Security自定义登录逻辑:UserDetailsService的作用

UserDetailsService介绍:

UserDetailsService接口如下:

 Spring Security 提供了以下实现:(基于内存、缓存和JDBC)

 上面接口返回一个UserDetails,这也是一个接口

public interface UserDetails extends Serializable {

	/**
	 * Returns the authorities granted to the user. Cannot return <code>null</code>.
	 * @return the authorities, sorted by natural key (never <code>null</code>)
	 */
	Collection<? extends GrantedAuthority> getAuthorities();

	/**
	 * Returns the password used to authenticate the user.
	 * @return the password
	 */
	String getPassword();

	/**
	 * Returns the username used to authenticate the user. Cannot return
	 * <code>null</code>.
	 * @return the username (never <code>null</code>)
	 */
	String getUsername();

	/**
	 * Indicates whether the user's account has expired. An expired account cannot be
	 * authenticated.
	 * @return <code>true</code> if the user's account is valid (ie non-expired),
	 * <code>false</code> if no longer valid (ie expired)
	 */
	boolean isAccountNonExpired();

	/**
	 * Indicates whether the user is locked or unlocked. A locked user cannot be
	 * authenticated.
	 * @return <code>true</code> if the user is not locked, <code>false</code> otherwise
	 */
	boolean isAccountNonLocked();

	/**
	 * Indicates whether the user's credentials (password) has expired. Expired
	 * credentials prevent authentication.
	 * @return <code>true</code> if the user's credentials are valid (ie non-expired),
	 * <code>false</code> if no longer valid (ie expired)
	 */
	boolean isCredentialsNonExpired();

	/**
	 * Indicates whether the user is enabled or disabled. A disabled user cannot be
	 * authenticated.
	 * @return <code>true</code> if the user is enabled, <code>false</code> otherwise
	 */
	boolean isEnabled();

}

Spring Security 提供了UserDetails 的实现类 User。

UserDetailService的作用

那么这个UserDetailService作用究竟是什么?通过debug分析:

1、写一个UserDetails的实现类,并注入到spring容器中:

package com.xxx.springsecuritydemo.service;

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author zongzhibin
 * @Date 2021/9/15 14:13
 */
@Service
public class UserService implements UserDetailsService {


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("");
        return new User("admin","123", AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}

2、启动项目,发现这个类调用的地方为

 注意这里只是返回一个User类,真正密码比对的地方在

additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken) authentication);

方法中:

 

结论:Spring Security 会将前端填写的username 传给 UserDetailService.loadByUserName 方法。我们只需要从数据库中根据用户名查找到用户信息然后封装为UserDetails的实现类返回给SpringSecurity 即可,咱们自己不需要进行密码的比对工作,密码比对交由SpringSecurity 处理。

关于PasswordEncoder:

当然咱们数据库中的用户的密码必须是通过SpringSecurity 指定的

PasswordEncoder 进行加密后保存,因为SpringSecurity 进行密码比对的时候会用相同的PasswordEncoder。

SpringSecurity没有默认的PasswordEncoder,需要用户指定:

 那生成用户的时候,密码进行加密:

 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Spring Security可以通过自定义登录页面来实现用户认证。具体步骤如下: 1. 创建一个登录页面,可以使用JSP、Thymeleaf等模板引擎来实现。 2. 在Spring Security配置文件中,配置登录页面的URL和处理登录请求的URL。 3. 在登录页面中,使用表单提交用户输入的用户名和密码。 4. 在Spring Security配置文件中,配置用户认证的方式,可以使用内存认证、数据库认证、LDAP认证等方式。 5. 在用户认证成功后,可以使用Spring Security提供的默认跳转页面,也可以自定义跳转页面。 6. 在用户认证失败后,可以在登录页面中显示错误信息,或者跳转到自定义的错误页面。 以上就是Spring Security自定义登录页面的基本步骤。 ### 回答2: Spring Security是一个用于认证和授权的框架,可以帮助我们实现安全的用户登录和权限管理。在Spring Security中,我们可以自定义登录页面来满足自己的需求。 首先,我们需要创建一个登录页面的JSP文件,可以放在项目的WEB-INF目录下。在该页面中,我们可以设计自己需要的输入框,例如用户名和密码的输入框,以及登录按钮。 接下来,在Spring Security的配置文件中,我们需要指定我们自定义登录页面。可以使用`http.formLogin().loginPage("/login")`来指定登录页面的URL路径。 然后,我们需要编写一个用于处理登录请求的Controller。该Controller需要处理用户输入的用户名和密码,并进行相应的验证。验证通过后,可以使用Spring Security提供的API来进行登录操作。 在登录验证成功后,我们可以根据需要进行跳转,例如跳转到用户的首页或者其他页面。 另外,我们还可以对登录页面进行一些额外的自定义操作,例如添加验证码功能、记住我功能等等。通过自定义登录页面,我们可以根据自己的需求对用户登录进行个性化设计,提供更好的用户体验。 总结起来,自定义登录页面就是通过创建一个自定义的JSP文件,在Spring Security的配置中指定该JSP文件的URL路径,然后编写一个处理登录请求的Controller来验证用户输入的用户名和密码,并进行相应的登录操作。自定义登录页面可以让我们更好地满足项目的需求,提高用户的登录体验。 ### 回答3: Spring Security是一个功能强大的安全框架,可以用于保护和管理Web应用程序的身份验证和授权。默认情况下,Spring Security提供了一个简单的登录页面,但我们可以根据我们的需求自定义登录页面。 首先,我们需要创建一个自定义登录页面。我们可以使用HTML、CSS和JavaScript来设计并构建登录页面。登录页面可以包含用户名和密码输入字段,以及登录按钮。可以添加额外的功能,如“忘记密码”链接或“注册”按钮,根据需求进行自定义。 在Spring Security的配置文件中,我们需要指定自定义登录页面的URL,并对其进行保护。我们可以使用Java配置或XML配置来完成这个步骤。例如,在Java配置中,我们可以使用`http.formLogin().loginPage("/custom-login")`来指定自定义登录页面的URL。 接下来,我们需要在自定义登录页面的表单中添加与Spring Security相关的字段。这些字段通常是“username”和“password”,以便Spring Security可以正确地验证用户凭据。 在后端,我们需要编写一个处理用户认证的逻辑。我们可以实现`UserDetailsService`接口来加载和验证用户的凭据。我们也可以自定义`AuthenticationProvider`来处理用户的认证请求,并提供自定义逻辑。 最后,我们需要将我们的自定义登录页面与Spring Security整合起来。我们可以在Spring Security的配置文件中添加一个身份验证过滤器,将自定义登录页面的URL与处理用户认证的逻辑连接起来。 总结起来,要自定义Spring Security登录页面,我们需要创建一个自定义登录页面,指定其URL,并对其进行保护。然后,我们需要在表单中添加与Spring Security相关的字段,并编写处理用户认证的逻辑。最后,将自定义登录页面与Spring Security整合起来。这样,我们就可以按照我们的需求来设计和实现自定义登录页面。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值