2013-10-19 11 views
10

私はウェブ上のガイドに従って、スプリングセキュリティを使用して自分のウェブサイトを保護しようとしています。だから、私のサーバー側でWebSecurityConfigurerAdapter、コントローラは、私は非常に混乱して何本スプリングセキュリティ403エラー

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter 
implements ApplicationContextAware { 

@Override 
protected void registerAuthentication(AuthenticationManagerBuilde r authManagerBuilder) throws Exception { 
authManagerBuilder.inMemoryAuthentication() 
.withUser("user").password("password").roles("ADMI N"); 
} 
} 

@Controller 
//@RequestMapping("/course") 
public class CourseController implements ApplicationContextAware{ 

@RequestMapping(value="/course", method = RequestMethod.GET, produces="application/json") 
public @ResponseBody List<Course> get(// The critirion used to find. 
@RequestParam(value="what", required=true) String what, 
@RequestParam(value="value", required=true) String value) { 
//..... 
} 

@RequestMapping(value="/course", method = RequestMethod.POST, produces="application/json") 
public List<Course> upload(@RequestBody Course[] cs) { 
} 
} 

のように見えることは、GETメソッドが正常に動作している間、サーバーは、POST/DELETEメソッドに応答しないです。ところで、私はクライアント側でRestTemplateを使用しています。例外は:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 403 Forbidden 
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) 
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:574) 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:530) 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:487) 
    at org.springframework.web.client.RestTemplate.delete(RestTemplate.java:385) 
    at hello.Application.createRestTemplate(Application.java:149) 
    at hello.Application.main(Application.java:99) 

私は数日間インターネットを検索しました。まだ手掛かりはありません。助けてください。ありがとうございます

+0

が正しいのですか?ロール(「ADMI N」)。 「I」と「N」の間にスペースがあります。 –

答えて

29

の可能性があります。ユーザーがWebブラウザでアプリケーションを使用しない場合は、then it is safe to disable CSRF保護。それ以外の場合はinclude the CSRF token in the requestにしてください。あなたは以下を使用することができますdisable CSRF protection

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig 
    extends WebSecurityConfigurerAdapter implements ApplicationContextAware { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      // ... 
      .csrf().disable(); 
    } 

    @Override 
    protected void registerAuthentication(AuthenticationManagerBuilde r authManagerBuilder) throws Exception { 
     authManagerBuilder 
      .inMemoryAuthentication() 
       .withUser("user").password("password").roles("ADMIN"); 
    } 
} 
+0

ありがとうございました。それはcsrfの問題です。はい、私はユーザーにWebブラウザを使用してアプリケーションを使用させたくありません。 クライアント側でうまくいきます。しかし、サーバー側で私はこれを取得します: o.s.web.servlet.PageNotFound:リクエストメソッド 'POST'はサポートされていません Strange。 私はそれを無視すべきですか? – ken

+1

私はPageNotFoundが私が扱う別の問題だと思います。おそらく、このエラーに関する詳細情報を含む別の質問を投稿する必要があります。 –