Java spring boot web应用程序中的JWT筛选器和spring安全控制流

Java spring boot web应用程序中的JWT筛选器和spring安全控制流,java,spring-boot,spring-security,jwt,Java,Spring Boot,Spring Security,Jwt,我对spring security很陌生。我试图在我的web应用程序中实现JWT过滤器,使其成为无状态的。我有一段代码,当用户点击/login路径时,控件转到方法 public LoginResponse login( AuthenticationRequest authenticationRequest, Device device ) throws WebappException { // Perform the security final Authent

我对spring security很陌生。我试图在我的web应用程序中实现
JWT
过滤器,使其成为无状态的。我有一段代码,当用户点击
/login
路径时,控件转到方法

public LoginResponse login( 
AuthenticationRequest authenticationRequest, Device device )
        throws WebappException
{
    // Perform the security
    final Authentication authentication = authenticationManager
            .authenticate(new 
UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(),
                    authenticationRequest.getPassword()));

SecurityContextHolder.getContext().setAuthentication(authentication);
/** some more logic**/
在这里,我不理解

最终身份验证=authenticationManager
.认证(新)
UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(),
authenticationRequest.getPassword())

请引导我!
AuthenticationRequest
有两个字段
userName
password

authenticationManager.authenticate()
方法将
UsernamePasswordAuthenticationToken
传递给
AuthenticationProvider
并尝试使用提供的用户名和密码对用户进行身份验证

  • 如果成功,它将返回一个具有授权权限的
    身份验证
    对象
  • 如果身份验证失败,它将引发异常
然后,您可以调用
authentication.isAuthenticated()
,以了解令牌是否已通过身份验证

如果要访问数据库以检查身份验证,应使用
DaoAuthenticationProvider
实现(或
AbstractUserDetailsAuthenticationProvider
)。 它从界面
UserDetailsService
检索用户详细信息。因此,您需要创建一个类
MyUserDetailsService
,该类实现
UserDetailsService
覆盖
loadUserByUsername(字符串用户名)
方法并返回
UserDetails
UserDetails
包含用户名、密码和权限。创建自己的
MyUserdetails
类,该类实现
UserDetails
接口。 然后,将Spring配置为引用cutom类:

DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(myUserDetailsService);
更多详情

或者,也可以使用JDBCUserDetailsManager配置器直接指定数据源和SQL查询:

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication().dataSource(dataSource)
         .usersByUsernameQuery("select username, password, enabled from users where username=?")
         .authoritiesByUsernameQuery("select username, role from user_roles where username=?");
}
关于JWT,我认为您的登录方法首先检查用户的身份验证,然后应该使用用户详细信息构建JWT并将其返回到浏览器。 然后客户端可以将这个令牌重新发送到服务器,这个JWT将被另一种方法解密和验证。
有关如何
final Authentication Authentication=authenticationManager.authenticate(新用户名passwordauthenticationtoken(authenticationRequest.getUsername(),authenticationRequest.getPassword())的详细信息执行用户名和密码检查?我只是传递用户在请求中提交的内容。如何根据数据库中的数据进行验证?