Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringSecurity用户详细信息服务获取密码_Java_Spring Security - Fatal编程技术网

Java SpringSecurity用户详细信息服务获取密码

Java SpringSecurity用户详细信息服务获取密码,java,spring-security,Java,Spring Security,我正在春季创建身份验证服务 我正在使用UserDetailsService获取表单变量,但我发现loadUserByUsername只有一个变量-userName 如何获取密码 public class userAuthentication implements UserDetailsService{ private @Autowired ASPWebServicesUtils aspWebServicesUtils; @Override public Use

我正在春季创建身份验证服务

我正在使用UserDetailsService获取表单变量,但我发现loadUserByUsername只有一个变量-userName

如何获取密码

public class userAuthentication implements UserDetailsService{

    private @Autowired
    ASPWebServicesUtils aspWebServicesUtils;

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

        //how to get password ?

        User user = new User("test", "test", true, true, true, true, getAuthorities(true));

        return user;  

    }

    private List<GrantedAuthority> getAuthorities(boolean isAdmin){

        List<GrantedAuthority> authorityList = new ArrayList<GrantedAuthority>(2);
        authorityList.add(new SimpleGrantedAuthority("USER_ROLE"));
        if(isAdmin){
            authorityList.add(new SimpleGrantedAuthority("ADMIN_ROLE"));
        }
        return authorityList;

    }
//...
}
public类userAuthentication实现userdetails服务{
私有@Autowired
ASPWebServicesUtils ASPWebServicesUtils;
@凌驾
public UserDetails loadUserByUsername(字符串名)引发UsernameNotFoundException{
//如何获取密码?
用户用户=新用户(“测试”、“测试”、真、真、真、真、GetAuthories(真));
返回用户;
}
私有列表GetAuthories(布尔isAdmin){
List authorityList=新的ArrayList(2);
添加(新的SimpleGrantedAuthority(“用户角色”);
如果(isAdmin){
添加(新的SimpleGrantedAuthority(“管理员角色”);
}
返回权威列表;
}
//...
}

谢谢

loadUserByUsername(字符串名)是在服务实现的接口(我想是userServicedetails)上定义的方法。您必须编写实现


正如您必须为getPassword()或类似的代码编写实现一样。。。spring并没有提供这一点。我想密码存储在你的用户对象中,但是你写了。。。您是否创建了
getPassword()
方法?

我相信
UserDetails服务
应该用于从一些后端存储、数据库、平面文件等获取
UserDetails
对象。一旦您创建了
UserDetails
,spring security(或您)就必须将其与用户名进行比较(或其他主体)和用户提供的密码(凭据),以便对该用户进行身份验证

我认为您没有按预期的方式使用它。

如果查看对象,构造函数中的第二个参数是密码

用于从后端结构(如数据库)加载用户。当用户尝试使用用户名和密码登录时,将调用该方法,然后服务负责加载用户定义并将其返回到安全框架。所需的详细信息包括
用户名
密码等数据e> ,
accountNonExpired
credentialsinexpired
accountNonLocked
权限

一旦spring security接收到用户对象,它将根据用户输入的密码和其他数据(如用户帐户状态(AccountNoExpired、CredentialsNoExpired等))验证用户。

一些用于检索用户信息和提供身份验证信息的标准(现成)机制包括:

  • inMemoryAuthentication
  • jdbcAuthentication
  • LDA验证
  • 用户详细信息服务
如果上述内容不符合您的目的,并且您需要一个自定义解决方案,您可以创建并配置一个新的身份验证提供程序,如下所示:

安全配置:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new CustomAuthenticationProvider());
    }

    ....
}
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
        String name = authentication.getName();
        // You can get the password here
        String password = authentication.getCredentials().toString();

        // Your custom authentication logic here
        if (name.equals("admin") && password.equals("pwd")) {
            Authentication auth = new UsernamePasswordAuthenticationToken(name,
                    password);

            return auth;
        }

        return null;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

}
身份验证提供程序:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new CustomAuthenticationProvider());
    }

    ....
}
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
        String name = authentication.getName();
        // You can get the password here
        String password = authentication.getCredentials().toString();

        // Your custom authentication logic here
        if (name.equals("admin") && password.equals("pwd")) {
            Authentication auth = new UsernamePasswordAuthenticationToken(name,
                    password);

            return auth;
        }

        return null;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

}
公共类CustomAuthenticationProvider实现AuthenticationProvider{
@凌驾
公共身份验证(身份验证)
抛出AuthenticationException{
String name=authentication.getName();
//你可以在这里获得密码
字符串密码=authentication.getCredentials().toString();
//这里是您的自定义身份验证逻辑
if(name.equals(“admin”)和password.equals(“pwd”)){
Authentication auth=新用户名PasswordAuthenticationToken(名称,
密码);
返回auth;
}
返回null;
}
@凌驾
公共布尔支持(类身份验证){
返回authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
XML实现:

<authentication-manager  alias="loginAuthenticationManager">
    <authentication-provider ref="loginAuthenticationProvider" />
</authentication-manager>

<!-- Bean implementing AuthenticationProvider of Spring Security -->
<beans:bean id="loginAuthenticationProvider" class="com.config.LoginAuthenticationProvider">
</beans:bean>

AuthenticationProvider:

public class LoginAuthenticationProvider implements AuthenticationProvider {
    @Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
        String name = authentication.getName();
        // You can get the password here
        String password = authentication.getCredentials().toString();

        // Your custom authentication logic here
        if (name.equals("admin") && password.equals("pwd")) {
            List<GrantedAuthority> grantedAuths = new ArrayList<>();
            grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
            return new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
        }
        return null;
    }
    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}
公共类LoginAuthenticationProvider实现AuthenticationProvider{
@凌驾
公共身份验证(身份验证)
抛出AuthenticationException{
String name=authentication.getName();
//你可以在这里获得密码
字符串密码=authentication.getCredentials().toString();
//这里是您的自定义身份验证逻辑
if(name.equals(“admin”)和password.equals(“pwd”)){
List grantedAuths=new ArrayList();
grantedAuths.add(新的SimpleGrantedAuthority(“角色用户”);
返回新的UsernamePasswordAuthenticationToken(名称、密码、grantedAuths);
}
返回null;
}
@凌驾
公共布尔支持(类身份验证){
返回authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
添加到web.xml

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

RequestContextHolder
基于
ThreadLocal

我有一个Web服务器,它提供用户存在的信息。我必须向这个Web服务器传递登录名和密码。你可能应该阅读
UserDetailsService
上的和以及它的用途。它只用于将数据加载到框架中。不回答你你的问题是,但是每当你有一个包含大量随机参数的构造函数时,它就是一个生成器模式的标志。例如:User.builder().accountExpired(false)。accountLocked(false)。credentialsfired(false)。roles(“ROLE_User”).disabled(false)。build();比新用户(…)更好我知道-我正在创建它。我希望接收以表单形式传递的数据。处理密码是spring框架的责任,安全框架将比较用户输入的密码和用户发送的密码service@ArunPJohny,我可以简单地将UserDetails命名为passwor