简体   繁体   English

使用angularjs消费安全的Spring Rest服务

[英]Consume secured spring rest service with angularjs

Well, I have a secured rest service just using the spring-security with static credentials 'user' and 'password'. 好吧,我仅使用带有静态凭据“用户”和“密码”的spring-security即可获得安全的休息服务。 When I consume the rest service shows the login dialog on the browser, that works fine. 当我使用其余服务时,在浏览器上显示登录对话框,效果很好。

The issue comes up when I want to consume my service using a web application using spring, I don't know how to proceed with that, I appreciate any solution, i'm kind of new in Spring, and I don't know if I have to add some method in my rest service or there is just a way to send the credentials from the webapp. 当我想使用使用Spring的Web应用程序来使用我的服务时出现问题,我不知道该如何进行,我很感激任何解决方案,我是Spring的新手,并且我不知道是否我必须在rest服务中添加一些方法,或者只有一种方法可以从webapp发送凭据。

This is my security config for my rest service. 这是我的休息服务的安全配置。

@Configuration
@Order(value = SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    private AuthenticationProvider authenticationProvider;
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests().anyRequest()
            .fullyAuthenticated().and().formLogin().permitAll();
    }
}

Im trying to consume the services using angular service: 我试图使用角度服务使用服务:

restwebapp.controller('restController', function($scope, Servicio){
    Servicio.GetGreeting.get({}, function(response){
        $scope.hello = response;
    });
})

restwebapp.factory('Servicio', function($resource){
    return{
        GetGreeting: $resource( 'http://localhost:8080/greeting', {}, {
        get:{   

            method:'GET', 
        }
        })
    }
})

The service returns the fromlogin in a json format, is there another way to authenticate to my rest service, or what is better way. 该服务以json格式返回fromlogin,还有另一种向我的rest服务进行身份验证的方法,或者还有什么更好的方法。

Thanks in advance 提前致谢

For a simple basic authentication without form, you can try this: 对于没有表单的简单基本身份验证,可以尝试以下操作:

@Override
protected void configure(HttpSecurity http) throws Exception {
     http
         .authorizeRequests().anyRequest()
         .fullyAuthenticated().and().httpBasic();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM