2017-03-13 8 views
-1

私はSpring Security 4 XMLベースの設定で作業しています。既存のXML Spring Securityの設定にREST基本認証を追加する方法は?

これは私の設定(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-3.0.xsd 
      http://www.springframework.org/schema/security 
      http://www.springframework.org/schema/security/spring-security.xsd"> 

    <http create-session="always" 
     use-expressions="true" 
     authentication-manager-ref="authenticationManager" 
     entry-point-ref="authenticationEntryPoint"> 
     <csrf disabled="true" /> 
     <intercept-url pattern="/**" access="hasRole('USER')" /> 
     <form-login authentication-success-handler-ref="customAuthenticationSuccessHandler" /> 
     <logout /> 
    </http> 

    <authentication-manager alias="authenticationManager"> 
     <authentication-provider user-service-ref="userDao"></authentication-provider> 
    </authentication-manager> 

</beans:beans> 

は私CustomEntryPointです:

@Component 
public class CustomEntryPoint implements AuthenticationEntryPoint { 

    @Override 
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { 
     System.out.println("Entering commence due to failed Authentication"); 
     response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized Access!"); 
    } 

} 

と私のUserDao(ファイルからの資格情報を読み込む将来のために):

public class UserDao implements UserDetailsService { 

    @Override 
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 

     String password = readPasswordFromFileOrDatabase(username); 

     if (password == null) throw new UsernameNotFoundException("failure"); 
     return User.withUsername("user").password(password).authorities("ROLE_USER").build(); 
    } 

    private String readPasswordFromFileOrDatabase(String username) { 
     if (username.equals("user")) return "q"; 
     return null; 
    } 


} 

そして、REST feautureがうまくいかないように見える、w私は、ユーザー/パスワードでhttp://localhost:8080/loginへの郵便配達を通じてPOSTを送る編:ユーザー/ Qそれは

悪い資格情報

しかし、私は、ブラウザでフォームを使用して同じことを行うとき、それは正常に動作を言います。

したがって、REST機能を動作させる方法はありますか?

答えて

1

次のコードは、ユーザー名とパスワードのbase64でエンコードされた値を示します。次のようにHTTPヘッダーを追加するためにそれを使用:このセットヘッダに

String plainClientCredentials="myusername:mypassword"; 
String base64ClientCredentials = new String(Base64.encodeBase64(plainClientCredentials.getBytes())); 
System.out.println(base64ClientCredentials);` 

キー - 認可、バリューBasicbase64ClientCredentials

仮定コードプリント123&78# その値がBasic123&78#になり。

+0

ヘッダーを追加した後も同じ動作をするため、動作しているかわかりません –

関連する問題