简体   繁体   English

对Spring的Angular登录请求以安全性为基础

[英]Angular login request to Spring rest with security

I am very new in Spring security. 我是Spring安全的新手。 I am confused about the Spring rest security but could not find any full solution. 我对Spring rest安全性感到困惑,但找不到任何完整的解决方案。 I have following scenario: 我有以下场景:

1) I have created the angular js service which makes a $http call to the spring rest. 1)我创建了角度js服务,它对spring rest进行$ http调用。

2) I want spring security to intercept this url(/login) and give me response back accordingly. 2)我希望spring security拦截这个url(/ login)并相应地给我回复。

What I tried if I am accessing the url directly http://localhost:8123/SpringMVC/login then its working fine, it's asking for username and password and after entering the correct user, password I got the result but same thing I am doing from AngularJs; 我尝试了如果我直接访问网址http:// localhost:8123 / SpringMVC / login然后它工作正常,它要求用户名和密码,输入正确的用户后,密码我得到了结果但是我做的一样来自AngularJs; it's giving me the following error 它给了我以下错误

angular.js:10514 OPTIONS http://localhost:8123/SpringMVC/rest/login/ (anonymous function) @ angular.js:10514sendReq @ angular.js:10333serverRequest @ angular.js:10045processQueue @ angular.js:14567(anonymous function) @ angular.js:14583$eval @ angular.js:15846$digest @ angular.js:15657$apply @ angular.js:15951bootstrapApply @ angular.js:1633invoke @ angular.js:4450doBootstrap @ angular.js:1631bootstrap @ angular.js:1651angularInit @ angular.js:1545(anonymous function) @ angular.js:28359trigger @ angular.js:2996eventHandler @ angular.js:3271
localhost/:1 XMLHttpRequest cannot load http://localhost:8111/SpringMVC/rest/categories/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8234' is therefore not allowed access. The response had HTTP status code 401.

Please suggest how to configure the header correctly in fronend as well as backend both angular and rest application are running on different server. 请建议如何在fronend中正确配置标头以及后端,角度和休息应用程序都在不同的服务器上运行。

This is in SecurityConfiguration.java 这是在SecurityConfiguration.java中

@Override
    protected void configure(HttpSecurity http) throws Exception {
         http
            .httpBasic()
          .and()
            .authorizeRequests()
              .antMatchers("/index.html", "/home.html", "/login.html", "/").permitAll()
              .anyRequest().authenticated();


    }

This is what I am doing in Entry Point: 这就是我在入口点所做的事情:

@Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException authException) throws IOException, ServletException {
    //prevent default behaviour
        if (request.getMethod().equals("OPTIONS")) {
         response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Methods", "POST,PUT, GET, OPTIONS, DELETE");
            response.addHeader("Access-Control-Max-Age", "3600");
            response.addHeader("Access-Control-Allow-Headers",
                    " Origin, X-Requested-With, Content-Type, Accept,AUTH-TOKEN");

        }
        else
        {
            System.out.println("hello from server");
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
        }
    }

Try adding- 尝试添加 -

response.setHeader("Access-Control-Request-Headers", "X-Requested-With, Content-Type, Accept");

Also update 还要更新

response.addHeader("Access-Control-Allow-Headers",
                    " Origin, X-Requested-With, Content-Type, Accept,AUTH-TOKEN");

to

response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,AUTH-TOKEN, Authorization");`

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

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