Spring安全性导致AngularJS发送GET请求而不是POST请求

时间:2016-08-12 03:03:12

标签: java angularjs spring rest spring-security

我有一个登录表单,由AngularJS登录控制器处理。我还有一个相应的Spring控制器,它从这个表单接收登录凭据。当我禁用Spring安全性时,此表单有效。我能够发送凭据,对数据库进行检查,并在前端接收用户对象。但是,当我使用WebSecurityConfigurerAdapter启用Spring安全性时,会导致出现此错误消息:

o.s.web.servlet.PageNotFound             : Request method 'GET' not supported

在Chrome开发者控制台中,它说" 405 GET不受支持"。

这是AngularJS控制器:

<div id="home" class="border-shadow" ng-app="landing">
    <div class="col-lg-5" ng-controller="authentication">
                        <form role="form">              
                            <div class="form-group row row-margin">
                                <div class="col-lg-5">
                                    <input type="text" class="form-control" ng-model="credentials.username" placeholder="Username" required>
                                </div>
                                <div class="col-lg-5">       
                                    <input type="password" class="form-control" ng-model="credentials.password" placeholder="Password" required>
                                    <a href="#" class="forget"> Forgot Your Password</a>
                                </div>
                                <div class="col-lg-2">
                                    <button ng-click="login()" class="btn btn-success">Log in</button>
                                </div>
                            </div>
                            <p>{{error}}</p>
                            <p>{{user.username}}</p>
                            <p>{{user.email}}</p>
                            <p>{{user.phoneNumber}}</p>
                        </form>  
      </div>
</div>

这里是WebSecurityConfigurerAdapter(让这个类导致来自AngularJS控制器的POST请求创建&#34; 405 GET不支持&#34;错误:

@Configuration
@EnableResourceServer
@Order(-20)
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    private final AuthenticationManager authenticationManager;

    @Autowired
    public CustomWebSecurityConfigurerAdapter(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.parentAuthenticationManager(authenticationManager);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http
        .authorizeRequests()
        .antMatchers("/css/**", "/js/**", "/images/**", "/fonts/**", "/videos/**")
        .permitAll()
        .and()
          .formLogin().loginPage("/login").permitAll()
        .and()
          .authorizeRequests()                      
          .antMatchers("/", "/join", "/login", "/about", "/contact", "/index.html")
          .permitAll()
        .and().authorizeRequests()
          .anyRequest().authenticated()                            
        .and().exceptionHandling()
          .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
        .and()
          .logout().logoutSuccessUrl("/index.html").permitAll()
        .and()
          .csrf().disable();
    }

}

这是Spring控制器:

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity<BusinessUser> login(@RequestBody BusinessUser inputUser) {
        BusinessUser requestedUser = businessUserService.findByUsername(inputUser.getUsername());
        if(requestedUser != null)
            if(BCrypt.checkpw(inputUser.getPassword(), requestedUser.getPassword()))
                return new ResponseEntity<>(requestedUser, HttpStatus.OK);
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

只是为了添加更多信息,这里是主要的:

@SpringBootApplication
@EnableJpaRepositories  ("com.test.app.repositories")
@EntityScan             ("com.test.app.model")
@ComponentScan          (basePackages={"com.test.app"})
@EnableWebSecurity
@EnableAutoConfiguration
public class App {

    public static void main( String[] args ) {
        SpringApplication.run(App.class, args);
    }

}

再次,澄清一下,当我没有WebSecurityConfigurerAdapter类时,登录系统完全正常。每当我添加这个类时,它都会导致不支持&#34; 405 GET&#34;当我用Angular控制器发送POST时,对我没有任何意义的错误。

编辑我的AngularJS控制器。 (对不起,忘记了):

<script>
    angular.module('landing', [])
    .controller('authentication',
      function($rootScope, $scope, $http, $location) {
      $scope.credentials = {};
      $scope.login = function() {
        $http.post('http://localhost:8090/login', $scope.credentials, {
          headers : {
            "content-type" : "application/json"
          }
        }).success(function(data, status) {
            $scope.user = data;
        }).error(function(data, status) {
            $scope.error = "Invalid username or password";
        })
      };
    });
</script>

0 个答案:

没有答案