SpringBoot示例未进行身份验证

时间:2015-07-01 16:18:15

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

我试图通过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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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