1

Spring Security 正在保护两个独立的 Spring Boot 后端服务,这些服务在两个独立的端口上运行,来自两个独立的 jar 文件。其中一项服务(称为resource,在此链接中包含完整代码)响应来自前端 Node.js 服务器的 GET 请求,但另一项服务(称为authserver,将在此链接中包含完整代码。)没有。 如何更改 spring 安全设置,以便authserver应用程序可以响应来自 node.js 服务器的请求?

这是来自 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 ");
    });

};

以下是nodemon显示的日志:
1.) 对authserver应用程序/uaa/user端点的调用给出了304响应(Spring Bootauthserver应用程序的日志没有显示任何接收请求的证据),以及
2.)resource应用程序的/resource端点返回了401响应( Spring Bootresource应用程序日志确实显示它收到了请求):

[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"}

我确认http://localhost:9999/uaa/user在 Web 浏览器中输入确实会触发 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 中,您使用.authorizeRequests().anyRequest().authenticated(). 这意味着每个请求都需要用户进行身份验证。

尝试在 GET /user 上更改 Spring Security 中的权限以授予未经身份验证的用户访问权限(如果您的 nodeJs 服务器在调用您的 Spring 服务时未发送 Auth token/header/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 回答