【问题标题】:Spring Security: how to retrieve username to accessDeniedPage?Spring Security:如何检索用户名以访问拒绝页面?
【发布时间】:2017-03-24 04:19:33
【问题描述】:

我正在使用 Spring Security、HTML 和 thymeleaf,我想检索登录失败的用户。登录有两个步骤:第一步检查用户和密码是否正确进入 LDAP,第二步检查用户是否在数据库中具有角色。只有当两者都通过时,用户才能通过身份验证。现在我想让登录失败的用户(预编译的输入字段)进入我的注册页面。注册页面是accessDeniedPage。这是身份验证:

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
            String name = authentication.getName();
            String password = authentication.getCredentials().toString();
            if (name == null || name.isEmpty() || password == null || password.isEmpty())
                return null;
            boolean isFind = ldapServices.ldapSearch(name, password);                           
            if (isFind){
                com.domain.User user = userServices.getByUsersEnabled(name);
                if (user!=null){
                    authorities.add(new SimpleGrantedAuthority("ROLE_"+user.getRole().getRole()));  
                }
                return new UsernamePasswordAuthenticationToken(user, password, authorities);
            }           
            else return null;
        }

重定向是:

@Override
protected void configure(HttpSecurity http) throws Exception {
    List<Role> roles=roleServices.getRoles();
    //Retrieve array of roles(only string field without id)
    String[] rolesArray = new String[roles.size()];
    int i=0;
    for (Role role:roles){
        rolesArray[i++] = role.getRole();
    }

    http
    .authorizeRequests() //Authorize Request Configuration
    //the "/register" path are accepted without login
    .antMatchers("/registration/**").authenticated()
    //all the path need authentication
    .anyRequest().hasAnyRole(rolesArray)//.authenticated()
    .and()//Login Form configuration for all others
    .formLogin()
    .loginPage("/login")
    .permitAll()
    .and()
    //redirect on page "registration" if user doens't exist in DART database
    .exceptionHandling().accessDeniedPage("/registration")
    .and()
    .logout()
    .logoutSuccessUrl("/login?logout")
    .deleteCookies("JSESSIONID", "JSESSIONID")
    .invalidateHttpSession(true)
    .permitAll();
}

如果我在 /registration 控制器中添加 Principal ,则它为空。为什么? Principal 为 null,可能是 authenticate 方法有问题。此外,我可能会遇到安全问题,因为我的注册 Web 服务没有经过身份验证,而它们应该在没有任何角色的情况下进行身份验证 登录失败的用户是否有可能进入注册页面? 谢谢

【问题讨论】:

  • 在我看来,您最后一个问题的答案是肯定的。默认情况下,如果有任何用户登录并且该用户无权访问任何页面,则您可以从 Principal 获取用户信息。
  • Principal return null,我也试过@AuthenticationPrincipal User user 和 sec:authentication="principal.username" 在 HTML 代码中,它们都是 null
  • 我从您的安全配置中了解到,如果任何注册用户想要打开您想要显示的注册页面,您无权访问此页面。是真的吗?
  • 如果用户进入 LDAP 但没有进入数据库(他将没有角色),那么他将被重定向到注册页面,我想在注册表中填写他的姓名(否则我应该要求用户名和密码)。用于表单的 Web 服务未经过身份验证,否则我会收到禁止错误
  • 也许我找到了问题,用户为空,因为它没有在数据库中找到它,所以查询返回空,我应该创建一个只有用户名的用户。现在我正在尝试

标签: java html spring spring-security thymeleaf


【解决方案1】:

我已将此行更改为身份验证方法:

return new UsernamePasswordAuthenticationToken(user==null?new User(name,null,false):user, password, authorities);

用户为空,因为他不存在于数据库中,所以查询返回空对象,相反我必须创建一个只有用户名的用户。

【讨论】:

    猜你喜欢
    • 2012-03-03
    • 2016-09-29
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多