2

Ich habe eine Spring Boot-Webanwendung mit Spring Initializer, eingebettet Tomcat, Thymeleaf Template-Engine und Paket als eine ausführbare JAR-Datei generiert.inMemoryAuthentication mit Spring Boot

Eingesetzte Technologien:

Frühling Stiefel 1.4.2.RELEASE, Frühling 4.3.4.RELEASE, Thymeleaf 2.1.5.RELEASE, Tomcat Embed 8.5.6, Maven 3, Java 8

Dies ist meine Sicherheitskonfigurationsklasse:

@Configuration 
@EnableWebSecurity 
@PropertySource("classpath:/com/tdk/iot/config/app-${APP-KEY}.properties") 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Value("${securityConfig.formLogin.loginPage}") 
    private String loginPage; 

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

     http 
      .formLogin() 
       .loginPage(loginPage) 
       .permitAll() 
       .loginProcessingUrl("/login") 
       .failureUrl("/login.html?error=true") 
       .defaultSuccessUrl("/books/list") 
       .and() 
      .exceptionHandling() 
       .accessDeniedPage("/denied") 
       .and() 
      .authorizeRequests() 
       .antMatchers("/mockup/**").permitAll() 
       .antMatchers("/books/**").permitAll() 
       .antMatchers("/welcome/**").authenticated() 
       .and() 
      .logout() 
       .permitAll() 
       .logoutSuccessUrl("/index.html"); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .inMemoryAuthentication() 
       .passwordEncoder(new StandardPasswordEncoder()) 
       .withUser("test1").password("test1").roles("ADMIN").and() 
       .withUser("test2").password("test2").roles("USER").and() 
       .withUser("test3").password("test3").roles("SUPERADMIN"); 
    } 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertyDefaultConfig() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 
} 

Hier die LoginController

@Controller 
public class LoginController { 

    @RequestMapping(value={ "/", "/tdk/login"}, method = { RequestMethod.POST,RequestMethod.GET}) 
    public String welcome(Map<String, Object> model) { 
     return "tdk/login"; 
    } 
} 

und die Vorlage:

<!DOCTYPE HTML> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head> 

<meta charset="utf-8" /> 
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> 
</head> 
<body> 

<div class="wrap"> 
    <div class="login"> 
     <div class="logo"></div> 

      <form th:action="@{/login.html}" method="post"> 

       <p th:if="${loginError}" class="error">Wrong user or password</p> 

       <div class="input_label"><i class="fa fa-user"></i><input type="text" name="user" placeholder="User" /></div> 
       <div class="input_label"><i class="fa fa-key"></i><input type="password" name="pass" placeholder="Password" /></div> 
       <input type="submit" value="LOGIN" /> 
      </form> 
     <div class="forget"> 
      <!-- <a href="#">Do you forgot your password?</a><br/> --> 
      <br/>    
     </div>   
    </div> 
</div> 

</body> 
</html> 

aber wenn ich mit test1/test1 Zugriff habe ich diesen Fehler:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Mar 05 20:16:11 CET 2017 There was an unexpected error (type=Method Not Allowed, status=405). Request method 'POST' not supported

+1

Kasse http://docs.spring.io/spring-security/site/docs/3.2.0.RELEASE/guides/form.html – hya

+0

BTW: Sie habe noch mehr Fehler in deinem Code. Die Standard-Parameternamen sind 'username' und' password', aber Sie verwenden 'user' und' pass' auf Ihrer Login-Seite. Sie müssen die Konfiguration oder die Anmeldeseite ändern. – dur

Antwort

1

Ihre Login-Seite ruft /login.html mit HTTP POST, aber Ihr Server bietet keine solche Anforderung Mapping.

Die konfigurierte URL in Ihrer Spring Security-Konfiguration:

.loginProcessingUrl("/login") 

wird die URL in Ihrer Login-Seite passend:

<form th:action="@{/login.html}" method="post"> 

Siehe auch AbstractAuthenticationFilterConfigurer#loginProcessingUrl:

Specifies the URL to validate the credentials.

-2

Standardmethode Controller für @RequestMapping ist GET, nicht POST.

Sie müssen die Methode im @requestMapping angeben.

@RequestMapping(value={ "/", "/tdk/login"}, method = RequestMethod.POST) 
+0

Ich habe das, dann: o.s.web.servlet.PageNotFound: Request-Methode 'GET' wird nicht unterstützt –

0

versuchen, diese Code

.failureUrl("/tdk/login?error=true") 

-Controller

@Controller 
public class LoginController { 

    @RequestMapping(value={ "/", "/tdk/login"},params = {"error"},method=RequestMethod.POST) 
    public String welcome(@RequestParam(value = "error", required = false) int error , ModelMap model) { 
if (error == 1) { 
      model.addAttribute("msg", "Invalid Username or Password"); 
      return "tdk/login"; 
     } 
else{ 
       return "redirect:home"; 

} 

    } 
}