当前位置: 首页 > 知识库问答 >
问题:

Spring security Remement me和自定义身份

巫培
2023-03-14
package com.websopti.wotms.auth;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.websopti.wotms.entity.User;
import com.websopti.wotms.enums.ErrorKey;
import com.websopti.wotms.service.UserService;

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    public UserService userService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        String email = authentication.getPrincipal().toString();
        String password = authentication.getCredentials().toString();

        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

        String username = null;

        try {
            username = userService.authenticate(email, password);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(username != null && !username.equals("0")){

            User user = userService.findByEmail(email);

            if (user != null) {

                authorities.add(new SimpleGrantedAuthority(user.getRole().name()));
                return (new UsernamePasswordAuthenticationToken(user, null, authorities));

            } else {

                User newUser = new User();
                newUser.setName(username);
                newUser.setEmail(email);
                newUser.setPassword(password);

                userService.register(newUser);

                newUser = userService.findById(newUser.getId());                
                authorities.add(new SimpleGrantedAuthority(newUser.getRole().name()));

                return (new UsernamePasswordAuthenticationToken(newUser, null, authorities));
            }

        } else {

            throw new UsernameNotFoundException(ErrorKey.USER_NOT_FOUND.name());

        }


    }

    @Override
    public boolean supports(Class<?> authentication) {

        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

}

如果我使用以下代码进行身份验证,那么它可以正常工作

package com.websopti.wotms.auth;

import org.springframework.beans.factory.annotation.Autowired;
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;

import com.websopti.wotms.entity.User;
import com.websopti.wotms.enums.ErrorKey;
import com.websopti.wotms.service.UserService;

@Service
public class AuthenticationService implements UserDetailsService {

    @Autowired
    public UserService userService;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {

        User user = userService.findByEmail(email);

        if(user != null)
            return user;
        else
            throw new UsernameNotFoundException(ErrorKey.USER_NOT_FOUND.name());
    }

}

有人能指导我代码中的问题是什么吗?

共有1个答案

堵景天
2023-03-14

根据医生的说法

请注意,这两个隐式都需要一个UserDetailsService。如果您使用的身份验证提供者不使用UserDetailsService(例如,LDAP提供者),那么除非您的应用程序上下文中也有UserDetailsService bean,否则它将无法工作。

示例默认remember-me使用TokenBasedRememberMeServices processAutoLoginCookie()方法从cookie中检索userDetails以验证用户/密码详细信息。所以您需要一个自定义的userDetailsService检查这个或这个

更新:

基本上记住我的功能做了两件事

  1. 成功登录后,它会创建一个“Rememeber-Me”浏览器cookie,其中包含基于用户名和密码的令牌(如果需要,可以计算令牌并将其保存在数据库中,也可以动态计算用户和密码的哈希值)。
  2. 当用户试图在没有任何会话的情况下访问受保护的页面(会话因超时而过期)时,RememberMe服务将通过从cookie中检索用户详细信息来尝试自动锁定用户,并在需要时验证用户详细信息。
 类似资料:
  • 我们正在研究在我们的应用程序中实施基于SAML的SSO身份验证,我想知道是否可以通过SAML指定自定义重定向URL。换句话说,我们可以在身份提供者中配置一个服务提供者,并让服务提供者通过SAML请求指定身份提供者在登录后应将用户重定向到哪里? 我们之所以要寻找这样的东西,是因为我们的应用程序运行在不同的服务器上,这取决于它们所处的开发阶段(开发、测试、暂存、生产)。如果我们不必为要迁移到SAML

  • 问题内容: 这是我的情况: 一个Web应用程序对许多应用程序执行某种SSO 登录的用户,而不是单击链接,该应用就会向正确的应用发布包含用户信息(名称,pwd [无用],角色)的帖子 我正在其中一个应用程序上实现SpringSecurity以从其功能中受益(会话中的权限,其类提供的方法等) 因此,我需要开发一个 自定义过滤器 -我猜想-能够从请求中检索用户信息,通过自定义 DetailsUserSe

  • 问题内容: 我可以使用Google帐户在AppEngine中对用户进行身份验证的方式非常好。 但是,我需要使用 自定义的身份验证登录系统 。 我将有一个AppUsers表,其中包含用户名和加密密码。 我阅读了有关gae会话的内容,但在启动应用安全性方面需要帮助。 如何跟踪经过身份验证的用户会话?设置cookie? 初学者。 问题答案: 您可以使用cookie来做到这一点……其实并不难。您可以使用C

  • 我在spring MVC项目中实现了一个自定义身份验证提供程序。在我自己的重载authenticate()方法中,我实现了自己的身份验证,其中我构造了自己的UserPasswordAuthenticationToken()并返回对象。 现在,上述对象“UserPasswordAuthentictionToken”中的用户ID被匿名化,密码为null,权限设置为授予该用户的权限。 问题: 这是否会导

  • 嗨,伙计们,我正在开发应用程序使用spring boot和spring security在我的应用程序中进行身份验证,我使用自定义令牌,并且我能够成功地对用户进行身份验证。现在我想添加自定义授权到我的应用程序我想通过以下方式进行授权: 我的自定义userDetails服务如下:

  • 我使用asp.net身份与索赔主体和索赔身份。标识由自定义用户管理器创建,并作为承载令牌发送给客户端。 一切正常,但是我想指示asp.net使用角色的自定义声明类型,而不是标准的System。安全。索赔。声明类型。作用(http://schemas.microsoft.com/ws/2008/06/identity/claims/role)。 有可能吗? 编辑:我想对角色构造函数使用标准的Auth