【问题标题】:SpringBoot sample not authenticatingSpring Boot 示例未进行身份验证
【发布时间】:2015-07-02 02:51:20
【问题描述】:

我正在尝试通过 Spring Boot 执行简单的 inMemoryAuthentication,但我收到“无效的用户名和密码”。在我提交登录表单后。同时,我无法在登录屏幕上看到任何静态内容。

package shell;
@SpringBootApplication
public class ShellApplication {

   public static void main(String[] args) {
    SpringApplication.run(ShellApplication.class, args);           
   }
}



package shell.mvc;
  @Configuration
  public class MvcConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/home").setViewName("home");
    registry.addViewController("/").setViewName("home");
    registry.addViewController("/hello").setViewName("hello");
    registry.addViewController("/login").setViewName("login");
  }
}

package shell.security;

  @Configuration
  @EnableWebMvcSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception{   
    http
            .authorizeRequests()
            .antMatchers("/","/home").permitAll()       
            .anyRequest().authenticated()
            .and()  
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll();

}


@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{

    auth
        .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");


    }

}

            <form th:action="@{/login}" method="post">
                <input type="text" name="username"/>
                <input type="password" name="Password"/>
                <button type="submit">Login</button>
           </form>

【问题讨论】:

    标签: spring spring-mvc authentication login spring-security


    【解决方案1】:

    我能够找到问题,密码名称属性是“密码”,带有大写字母 P 而不是“密码”。第二个问题仍然是如何允许登录页面访问静态内容,如 /images 和 /css。

    【讨论】:

      【解决方案2】:

      允许访问登录页面上的静态内容如下:

            antMatchers("/css/**","/js/**","/fonts/**","/images/**","/","/home").permitAll()
      

      【讨论】:

        猜你喜欢
        • 2018-08-29
        • 2023-03-19
        • 1970-01-01
        • 2020-12-28
        • 2020-08-07
        • 2014-09-22
        • 1970-01-01
        • 1970-01-01
        • 2022-09-28
        相关资源
        最近更新 更多