14

他の Web サービスで使用される安らかな Web サービスを作成しようとしています。理想的には、クライアントがサービスにアクセスし、認証されていない場合、401 を取得する必要があります。リクエストに認証ヘッダーを追加して、ユーザーを認証できるようにしたいと考えています。ユーザーがログインフォームに記入して投稿したくありません。また、ログイン資格情報をCookieに保存したくない(つまり、状態を保持する)必要はありません。すべてのログイン資格情報は、各リクエストで送信される認証ヘッダーに含まれている必要があります。spring roo を使用して Web サービスを作成しました。

私が現在持っているもの (Spring Security 3.1 チュートリアルの 1 つから取得) では、ユーザーが 401 を取得すると、ログイン ページが表示され、ページを投稿して、リクエストごとに送信される Cookie を取得します。

これが私の春のセキュリティxmlです。

 <http use-expressions="true">
   <intercept-url pattern="/customers/**" access="isAuthenticated()" />
 <intercept-url pattern="/**" access="denyAll" />
<form-login />
 </http>
<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="alice" password="other" authorities="user" />
            <user name="custome1" password="other" authorities="user" />
        </user-service>
    </authentication-provider>
</authentication-manager>

curl からリクエストを送信すると、次のようになります。

 $ curl -i -H "Accept: application/json" -H "Authorization: Basic Y3VzdG9tZXIxOm90aGVy" http://localhost:8080/Secured/customers

 HTTP/1.1 302 Found
 Server: Apache-Coyote/1.1
 Set-Cookie: JSESSIONID=B4F983464F68199FA0160DBE6279F440; Path=/Secured/; HttpOnly
 Location:      http://localhost:8080/Secured/spring_security_login;jsessionid=B4F983464F68199FA0160DBE6279F440
 Content-Length: 0
 Date: Thu, 25 Apr 2013 17:18:48 GMT

私の基本的なヘッダートークンは base64(customer1:other) です

Web サービスが認証ヘッダーを受け入れ、ログイン ページにリダイレクトしないようにするにはどうすればよいですか?

security.xml から を削除すると、次のようになります。

excpetion:org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
    Configuration problem: No AuthenticationEntryPoint could be established.
    Please make sure you have a login mechanism configured through the namespace
    (such as form-login) or specify a custom AuthenticationEntryPoint with the
    'entry-point-ref' attribute
4

3 に答える 3

9

削除<form-login>して追加する必要がありました<http-basic>。また、config に create-session-"stateless" を追加しました。

 <http use-expressions="true" create-session="stateless" >
     <intercept-url pattern="/customers/**" access="isAuthenticated()" />
     <intercept-url pattern="/**" access="denyAll" />
     <http-basic/>
 </http>
于 2013-04-25T17:48:29.327 に答える
9

以下は、Rest 認証用の非 xml 構成の例、または必要に応じてフォームまたは基本的なものを使用したものです。

.and().httpBasic(); 

魔法をかけます!)))

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/someurl").hasRole("ADMIN")
            .antMatchers("/anotherurl").hasRole("USER")
            .antMatchers("/", "main").permitAll().anyRequest().authenticated()
            .and()
            .httpBasic();

        http.formLogin()
            .loginPage("/login").permitAll()
            .and()
            .logout().permitAll();
    }
...
}
于 2014-11-12T01:00:31.967 に答える