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

时间:2018-03-01 11:06:13

标签: angularjs spring authentication spring-security

我无法找到登录的响应

好的,这是我的映射

locahost: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

我的webconfig

@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

1 个答案:

答案 0 :(得分:-1)

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