$ http get使用AngularJS返回html而不是JSON Object Spring Security

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了$ http get使用AngularJS返回html而不是JSON Object Spring Security相关的知识,希望对你有一定的参考价值。

我找不到登录的响应

好的,这是我的映射

localhost:8080 /是我的index.html但如果没有登录页面将被重定向到/ login

但问题是我无法通过我的函数,因为response.data的返回是html本身

我只是按照教程并将其与我的webapp合并,但似乎无法做到正确

在我登录enter image description here之后

你看我需要获取response.data以获取身份验证信息

$scope.login = function () {

        var base64Credential = btoa($scope.username + ':' + $scope.password);

        // calling GET request for getting the user details
        console.log($scope.username);
        console.log($scope.password);
        console.log(base64Credential);

        $http.get('user', {headers: {'Authorization': 'Basic ' + base64Credential}}).then(function successCallback(response) {
            $scope.password = null;
            console.log(response);
            if (response.data.authenticated) {

                $scope.message = '';
                // setting the same header value for all request calling from this app
                $http.defaults.headers.common['Authorization'] = 'Basic ' + base64Credential;
                $scope.user = response.data.principal;
                //$sessionStorage.authenticate = $http.defaults.headers.common['Authorization'];
                //$sessionStorage.showLogin = $scope.showLogin = true;
                //$sessionStorage.user = $scope.user;
                $scope.isLogin = true;
                console.log(response.data.principal.role);
                //if (response.data.principal.role != "ADMIN") {
                //    $scope.notAdmin = true;
                //}

                var host = $window.location.host;
                var landingUrl = "http://" + host + "/";
                $window.location.href = landingUrl;
            } else {

                $scope.message = 'Authetication Failed !';
            }
        }, function errorCallback(response) {

            return response;
            //error
        });
    }

这是我的MainController.java enter image description here

我的网络配置

@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    auth
            // using in memory authentication
            .inMemoryAuthentication()
                    // adding Administrator user
            .withUser("admin").password("admin").authorities("ADMIN")
            // using add() method for adding new user
            .and()
                    // adding a user without Administrator authority
            .withUser("user").password("user").authorities("USER");
}



@Override
public void configure(WebSecurity web) throws Exception {
    super.configure(web);
}


@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            // starts authorizing configurations
    .csrf().disable()
            .authorizeRequests()
            .antMatchers("/css/**", "/js/**", "/images/**", "/font/**", "/libs/**").permitAll()
            .antMatchers("/**").hasAnyRole("ADMIN")
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll()
                .and()
                .exceptionHandling().accessDeniedPage("/403");

    }

第一个控制台是我当前的项目第二个控制台是我的旧项目,具有相同的代码enter image description here

/ user返回html wth :( enter image description here

答案

而不是使用@Controller尝试@RestController。您可能还需要在WebSecurityConfigurerAdapter中添加.httpBasic()

以上是关于$ http get使用AngularJS返回html而不是JSON Object Spring Security的主要内容,如果未能解决你的问题,请参考以下文章

$ http get使用AngularJS返回html而不是JSON Object Spring Security

AngularJS:POST请求返回302后发送的GET请求用于登录

AngularJS 工厂 http 返回空

在 AngularJS 中缓存 HTTP 'Get' 服务响应?

在AngularJs中从控制器调用服务时,从$ get factory方法返回一个值

Angular JS $http.get 从 WP REST API 返回空数据,但仅在实际 iOS 设备上