【问题标题】:Spring Security - Authentication issueSpring Security - 身份验证问题
【发布时间】:2019-04-08 19:43:07
【问题描述】:

我正在开发一个 Web 应用程序并选择使用 Spring Security。这个想法是让用户通过身份验证以查看主页,如果用户未通过身份验证,他们将被重定向到登录页面。此登录页面还显示了一个注册表单的链接,这部分工作正常。

但是,我在尝试允许用户通过注册链接注册时遇到了问题。如果用户未通过身份验证(“showRegistrationForm”),则无法访问注册表单的链接

谁能提供关于为什么会发生这种情况的见解?我在下面的 SecurityConfig 中包含了代码 sn-p

    @Override
protected void configure(HttpSecurity http) throws Exception {

    //Restrict Access based on the Intercepted Servlet Request 

    http.authorizeRequests()
    .antMatchers("/resources/**", "/register").permitAll()
    .anyRequest().authenticated()
    .antMatchers("/").hasRole("EMPLOYEE")
    .antMatchers("/showForm/**").hasAnyRole("EMPLOYEE","MANAGER", "ADMIN")
    .antMatchers("/save/**").hasAnyRole("MANAGER", "ADMIN")
    .antMatchers("/delete/**").hasRole("ADMIN")
    .and()
    .formLogin()
// Show the custom form created for the below request mappings
        .loginPage("/showSonyaLoginPage")
        .loginProcessingUrl("/authenticateTheUser")
// No need to be logged in to see the login page
        .permitAll()
    .and()
// No need to be logged in to see the logout button.
    .logout().permitAll()
    .and()
    .exceptionHandling().accessDeniedPage("/access-denied");    
}

【问题讨论】:

  • 它看起来与注册时触发的操作有关。可以看看注册后触发了什么动作吗?
  • 嗨阿米达拉,我已将注册码添加到原帖中。
  • 映射到 registration-confirmation 的视图是什么? (假设你使用的是 spring-mvc)
  • 注册确认显示一个基本的“注册成功”页面和一个返回用户登录的链接。用户输入用户名和密码即可成功登录,并将其带到“主页”
  • 我想我想从“注册页面”中删除身份验证并允许任何人看到它,但是 .permiteall() 似乎没有做这项工作

标签: spring-boot spring-security


【解决方案1】:

修改如下代码:

@Override
protected void configure(HttpSecurity http) throws Exception {


    // Restrict Access based on the Intercepted Servlet Request 

    http.authorizeRequests()
        .antMatchers("/showRegistrationForm/").permitAll()
        .anyRequest().authenticated()
        .antMatchers("/").hasRole("EMPLOYEE")
        .antMatchers("/resources/").permitAll()
        .antMatchers("/showForm/**").hasAnyRole("EMPLOYEE","MANAGER", "ADMIN")
        .antMatchers("/save/**").hasAnyRole("MANAGER", "ADMIN")
        .antMatchers("/delete/**").hasRole("ADMIN")
        .and()
        .formLogin()
    // Show the custom form created for the below request mappings
            .loginPage("/showSonyaLoginPage")
            .loginProcessingUrl("/authenticateTheUser")
    // No need to be logged in to see the login page
            .permitAll()
        .and()
    // No need to be logged in to see the logout button.
        .logout().permitAll()
        .and()
        .exceptionHandling().accessDeniedPage("/access-denied");    
}

下移以下代码:

anyRequest().authenticated()

【讨论】:

  • 嘿伙计,我尝试了您提供的代码,遗憾的是它产生了相同的结果。感谢您的尝试:)
猜你喜欢
  • 1970-01-01
  • 2016-05-29
  • 2014-05-11
  • 2013-05-13
  • 2016-12-22
  • 2011-10-20
  • 2016-11-02
  • 2022-01-15
  • 2014-02-26
相关资源
最近更新 更多