2016-08-23 4 views
0

GETメソッドを使用してSpringセキュリティを使用して認証しようとしています。 私はPOSTリクエストに使用しています。これは問題を引き起こしていますか?誰かが問題に助けてくれますか?Springセキュリティ:PUT/POSTリクエストで現在認証されているユーザにアクセスします。

GETメソッド

@RequestMapping(value = "/getuser", method = RequestMethod.GET) 
@ResponseBody 
public String getAuthenticatedUser(){ 
Authentication user =(Authentication)SecurityContextHolder.getContext() 
    .getAuthentication(); 
String userName = user.getUser().getUsername(); 
return userName; 
} 
} 

POSTメソッド

@RequestMapping(value = "/rest/getuser", method = RequestMethod.POST) 
@ResponseBody 
public String getAuthenticatedUser(){ 
Authentication user =(Authentication)SecurityContextHolder.getContext() 
    .getAuthentication(); 
String userName = user.getUser().getUsername(); 
return userName; 
} 
} 

春のsecurity.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/security 
     http://www.springframework.org/schema/security/spring-security.xsd"> 
<!--HTTP Interceptors for authentication --> 
<http pattern="/templates/**" security="none"></http> 
<http pattern="/css/**" security="none"></http> 
<http pattern="/js/**" security="none"></http> 
<http pattern="/lib/**" security="none"></http> 
<http pattern="/lib/css/**" security="none"></http> 
<http pattern="/lib/js/**" security="none"></http> 
<http pattern="/lib/fonts/**" security="none"></http> 
<http pattern="/img/**" security="none"></http> 
<http pattern="/rest/**" security="none"></http> 
<http pattern="/oAuth" security="none"></http> 
<http entry-point-ref="entryPoint" 
    auto-config="true" use-expressions="true"> 
    <anonymous enabled="false"></anonymous> 
    <custom-filter ref="oAuthFilter" after="SECURITY_CONTEXT_FILTER"></custom-filter> 
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')"></intercept-url> 
</http> 

<authentication-manager alias="upmAuthenticationManager"></authentication-manager> 
<beans:bean id="entryPoint" class="auth.EntryPoint"> 
    <beans:constructor-arg value="/index.html"></beans:constructor-arg> 
</beans:bean> 

<beans:bean id="oAuthEnd" name="auth.oAuthEnd" 
    class="oAuth.OAuthServlet"> 
    <beans:property name="oAuthFilter" ref="oAuthFilter"></beans:property> 
</beans:bean> 
<beans:bean id="oAuthFilter" class="auth.filter"> 
    <beans:property name="id" 
     value=""></beans:property> 
    <beans:property name="secret" 
     value=""></beans:property> 
    <beans:property name="url" 
     value=""></beans:property> 
</beans:bean> 

答えて

0

リクエスト方法はしていませんそれと関係するヒンジ。しかし、この行:

<http pattern="/rest/**" security="none"></http> 

それはそのパスのために取り扱うすべてセキュリティを無効にします。

はたぶん、あなたは意図:

<intercept-url pattern="/rest/**" access="permitAll"/> 
+0

私が削除した場合、。 PUT/POSTリクエストで403の禁止エラーが発生しています – Nagendra

+0

@Nagendra CSRFエラーですか? – holmis83

+0

私も同じことを考えて、http://spring.io/blog/2013/08/21/spring-security-3-2-0-rc1-highlights-csrf-protectionにあるアプローチを試しました。しかし、ヘッダーにCSRFトークンを取得できません(私は角度js $ http要求を使用しています) – Nagendra

関連する問題