2015-10-14 4 views
0

J'ai ajouté le ressort «spring-boot-starter-security» au projet existant de botte de ressort; après que les méthodes de poste dans le contrôleur de repos de printemps ne fonctionne pas correctement, il montre une chose d'erreur comme ceci:les méthodes de poteau de sécurité de démarreur de démarrage de ressort ne fonctionnent pas

o.s.web.servlet.PageNotFound    : Request method 'POST' not supported 


Remote Address:127.0.0.1:8080 
Request URL:http://localhost:8080/authenticate 
Request Method:POST 
Status Code:405 Method Not Allowed 
Response Headers 
view source 
Allow:GET, HEAD 
Cache-Control:no-cache, no-store, max-age=0, must-revalidate 
Content-Type:application/json;charset=UTF-8 
Date:Wed, 14 Oct 2015 05:41:06 GMT 
Expires:0 
Pragma:no-cache 
Server:Apache-Coyote/1.1 
Transfer-Encoding:chunked 
X-Content-Type-Options:nosniff 
X-Frame-Options:DENY 
X-XSS-Protection:1; mode=block 
Request Headers 
view source 
Accept:application/json, text/plain, */* 
Accept-Encoding:gzip, deflate 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:47 
Content-Type:application/json;charset=UTF-8 
Cookie:_ga=GA1.1.630164096.1442901791; JSESSIONID=B9F1946DAE5BCA7772526CFC735616EC 
Host:localhost:8080 
Origin:http://localhost:8080 
Referer:http://localhost:8080/ 
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36 
X-ZUMO-APPLICATION:GSECUHNQOOrCwgRHFFYLXWiViGnXNV88 
Request Payload 

ma méthode de commande est la suivante:

@RequestMapping(value="/authenticate",method = {RequestMethod.POST}) 
public LinkedHashMap<String,Object> authenticate(@RequestBody UserCredentials user){ 


    LinkedHashMap<String, Object> res =new LinkedHashMap<String, Object>(); 
    //business logic 
    return res; 
} 

fichier pom:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.dfasfa</groupId> 
    <artifactId>AdminDashboard</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <name>AdminDashboard</name> 


    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.2.4.RELEASE</version> 
     <relativePath /> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
     <java.version>1.7</java.version> 
    </properties> 

    <dependencies> 
      <dependency> 
      <groupId>javax.validation</groupId> 
      <artifactId>validation-api</artifactId> 
      <version>1.1.0.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>5.0.3.Final</version> 
     </dependency> 

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-tomcat</artifactId> 
    <!--<scope>provided</scope>--> 
</dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 



     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.21</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.tomcat.embed</groupId> 
      <artifactId>tomcat-embed-jasper</artifactId> 
      <!--<scope>provided</scope>--> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <!--<scope>provided</scope>--> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-jpamodelgen</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-integration</artifactId> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

</project> 


@Configuration 
@ComponentScan 
@EnableWebMvcSecurity 

public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

@Autowired 
private UserDetailsService userDetailsService; 

@Autowired 
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(userDetailsService).passwordEncoder(passwordencoder()); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests() 
      .antMatchers("/addService").permitAll() 
      .antMatchers("/app/*").permitAll() 
      .antMatchers("/lib/*").permitAll() 
      .antMatchers("/css/*").permitAll() 
      .antMatchers("/styles/*").permitAll() 
      .antMatchers("/login").permitAll() 
      .antMatchers("/getAllUsers").permitAll() 
      .anyRequest().permitAll() // for testing 
    .and() 
// .formLogin().loginPage("/login") 
      .formLogin() 
    .usernameParameter("username").passwordParameter("password") 
    .and() 
    .logout().logoutSuccessUrl("/login?logout") 
    .and() 
    .exceptionHandling().accessDeniedPage("/#/login") 
    .and() 
    .csrf(); 
} 

@Bean(name="passwordEncoder") 
    public PasswordEncoder passwordencoder(){ 
    return new BCryptPasswordEncoder(); 
    } 





} 


import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.ApplicationContext; 

@SpringBootApplication 
public class Application { 


    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(Application.class, args); 
     System.out.println("Let's inspect the beans provided by Spring Boot:");    

    } 

} 

Si je supprime toutes ces choses liées à la sécurité, le code fonctionne très bien. Y at-il quelque chose qui ne va pas dans ma configuration? . le formulaire de connexion de sécurité et toutes les autres fonctionnalités fonctionnent très bien. mais les méthodes post ne fonctionnent pas.

Répondre

1

.csrf().disable()

0

Pourriez-vous donner un aperçu de ce qu'est l'objet UserCredentials? Je ne peux pas le voir affiché dans la demande aussi.

Qu'est-ce que vous essayez de faire plutôt me fait penser à ce que vous pouvez réaliser en utilisant l'annotation @AuthenticationPrincipal plutôt que l'affichage du contenu avec un @RequestBody. Cela impliquera encore d'autres mécanismes.

Quoi qu'il en soit, deux choses:

  1. Que voulez-vous revenir de ce contrôleur? N'y at-il pas un @ResponseBody manquant dans la méthode d'authentification? Pourriez-vous essayer d'ajouter cela?
  2. Juste au cas où: Pourriez-vous essayer de faire respecter le type de contenu consommé par votre contrôleur (consume attribut du @RequestMapping annotation) comme envoyé dans votre demande (déjà mentionné que l'application/JSON)
0

Plutôt que de désactiver une fonctionnalité de sécurité, il est préférable de rechercher comment l'implémenter correctement. http://www.baeldung.com/spring-security-csrf

En bref, vous devez ajouter un attribut caché à tous vos formulaires POST.

<input type="hidden" th:attr="name=${_csrf.parameterName},value=${_csrf.token}"/>