1

Spring Security は、2 つの別個の jar ファイルから、2 つの別個のポートで実行されている 2 つの別個の Spring Boot バックエンド サービスを保護しています。サービスの 1 つ ( と呼ばれるresource、完全なコードはこのリンクにあります) はフロントエンド Node.js サーバーからの GET 要求に応答しますが、もう 1 つのサービス ( と呼ばれる、このリンクにある完全なauthserverコードはあります) は応答しません。 アプリが node.js サーバーからのリクエストに応答できるように、Springのセキュリティ設定を変更するにはどうすればよいですか?authserver

以下は、2 つのバックエンド サービスにリクエストを送信する node.js サーバーからの express.js コードです。

var url = require('url');
var request = require('request');

// expose the routes to our app with module.exports
module.exports = function(app) {

    // application --------------------------------
    app.get('/resource/**', function(req, res) {
        request.get('http://localhost:9000/resource', function (error, response, body) {
            if(error){console.log('ERROR with resource request.')}
            if (!error){// && response.statusCode == 200) {
                console.log(response.statusCode);
                console.log(body);
            };
        });
        console.log("You Hit The Resource Route ");
    });

    app.get('/user/**', function(req, res) {
        request.get('http://localhost:9999/uaa/user', function (error, response, body) {
            if(error){console.log('ERROR with user request.')}
            if (!error){// && response.statusCode == 200) {
                console.log(response.statusCode);
                console.log(body);
            };
        });
        console.log("You Hit The User Route ");
    });

};

1.)アプリのエンドポイント への呼び出しが応答を返したこと ( Spring Bootアプリのログは、要求を受信した証拠をnodemon示していない)、および 2.)アプリのエンドポイントが応答を返したこと ( Spring Bootアプリのログは、リクエストを受信したことを示しています):
authserver/uaa/user304authserver
resource/resource401resource

[nodemon] starting `node server.js`
App listening on port 8080
GET /user 304 4.257 ms - -
You Hit The Resource Route 
401
{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

Web ブラウザーに入力http://localhost:9999/uaa/userすると、Spring Bootauthserverアプリがトリガーされ、リクエストを文書化したログが作成され、ブラウザーに次の xml が表示 されることを確認しました。

<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>
4

1 に答える 1

1

AuthserverApplication.java で、Spring セキュリティを でオーバーライドしまし.authorizeRequests().anyRequest().authenticated()た。つまり、すべてのリクエストでユーザーが認証される必要があります。

GET /user の Spring Security の権限を変更して、認証されていないユーザーにアクセスを許可してみてください (Spring サービスを呼び出すときに nodeJs サーバーが認証トークン/ヘッダー/Cookie を送信しない場合)。

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
             .formLogin().loginPage("/login").permitAll()
        .and()
            .requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
        .and()
            .authorizeRequests()
            .antMatchers(HttpMethod.GET, "/user").permitAll()
            .anyRequest().authenticated();
        // @formatter:on
    }
于 2016-07-01T07:20:49.997 に答える