【问题标题】:How works loadUserByUsername method of UserDetails ( Mine have empty username)?UserDetails 的 loadUserByUsername 方法如何工作(我的用户名为空)?
【发布时间】:2014-10-30 21:13:37
【问题描述】:

我已经在我的应用程序中配置了 Spring Security。我想测试我可以登录我的应用程序,我让默认登录网址。这是我的弹簧配置文件:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
    <global-method-security pre-post-annotations="enabled" />
 <http pattern="/resources/**" security="none" />

 <http auto-config="true" use-expressions="true">

    <intercept-url pattern="/user/*" access="hasRole('ROLE_USER')"/>
    <intercept-url pattern="/admin/*" access="hasRole('ROLE_ADMIN')"/>
 </http>

<authentication-manager>
            <authentication-provider user-service-ref="customUserDetailsServiceImpl"/>
</authentication-manager>           
</beans:beans>

在我的 customerUserDetailsS​​erviceImpl 代码下方:

@Service
public class CustomUserDetailsServiceImpl implements UserDetailsService{

    @Autowired
    private IUserServices userServices;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        User user = userServices.findUserByEmail(email);

        if (user == null) {
            //LOGGER.error("No user found with username: " + username);
            throw new UsernameNotFoundException("No user found with username: " + email);
        }
        //TODO change 
         boolean enabled = true;  
          boolean accountNonExpired = true;  
          boolean credentialsNonExpired = true;  
          boolean accountNonLocked = true;  

          UserDetails userDetails =  new org.springframework.security.core.userdetails.User(user.getEmail(),user.getPassword(),enabled,accountNonExpired, credentialsNonExpired,accountNonLocked,getAuthorities(user.getRole())); 

          Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
          SecurityContextHolder.getContext().setAuthentication(authentication);
          return userDetails;
    }

    public Collection getAuthorities(Role role) {
           // public Collection<!--? extends GrantedAuthority--> getAuthorities(Long role) {  
                List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role));  
                return authList;  
            }  




    // TODO AMELIORER
    public List<String> getRoles(Role role) {  

        List<String> roles = new ArrayList<String>();  

        if (role.equals("ROLE_ADMIN")) {  
            roles.add("ROLE_ADMIN");  
            roles.add("ROLE_USER");

        } 
        else if(role.equals("ROLE_USER")){
            roles.add("ROLE_USER");
        }
        return roles;  
    }  

    public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles) {  
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();  

        for (String role : roles) {  
            authorities.add(new SimpleGrantedAuthority(role));  
        }  
        return authorities;  
    }  

      }

我在网址上发帖请求

结果我有 200 个 http 代码,调试后我注意到在线

User user = userServices.findUserByEmail(email);

在我的自定义用户详细信息服务中,电子邮件为空。为什么这个 ?如何将用户名和密码提供给 userdetails ? 感谢您的帮助

【问题讨论】:

  • 看来这是同一个问题stackoverflow.com/questions/24270421/…可能有用
  • 谢谢我刚刚看了你的回答,但不是同一个问题
  • 不要在你的服务中设置上下文!!!只检索用户并让 Springhandle 其余的。它不能为空,因为您总是在分配它-> email = "dkiala@gmail.com";(重新分配方法参数是不好的做法恕我直言)。
  • 添加所有项目文件的完整源代码。我会运行它来模拟你的问题。
  • M.Deinum 我添加了电子邮件以测试我是否能够获取用户详细信息。所以我的用户名仍然为空

标签: spring spring-mvc spring-security


【解决方案1】:

/**和/*的区别在类说明org.springframework.util.AntPathMatcher中有很好的解释,有很好的例子,看看

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2023-04-03
    • 2012-06-11
    • 2021-03-04
    • 2016-04-27
    • 2021-12-20
    • 2012-02-04
    相关资源
    最近更新 更多