简体   繁体   English

Spring 安全和 REST API 登录

[英]Spring security and REST API login

I have backend in spring-boot, exposing REST API, and i have SEPARATED front end in react.我在 spring-boot 中有后端,暴露了 REST API,并且我在反应中有分离的前端。

I want to use spring security to secure my back end, and i want to keep spring authentication chain.我想使用 spring security 来保护我的后端,并且我想保留 spring 身份验证链。 However, when i use configuration such as:但是,当我使用以下配置时:

@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                .cors().disable()
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/newUser").permitAll()
                .anyRequest().authenticated()
                .and()
                .exceptionHandling()
                .and()          
                .formLogin().pertmitAll();
    }

This generates /login endpoint and every request that is not authorized will get redirected to login page.这会生成/login端点,并且每个未经授权的请求都将被重定向到login页面。 However spring generates this /login login page and as i said i have my own stand-alone fornt-end in REACT, so i do not want spring to generate this page, i want it to tell my client to redirect to /login .然而,spring 生成了这个/login登录页面,正如我所说,我在 REACT 中有自己的独立前端,所以我不希望 spring 生成这个页面,我希望它告诉我的客户重定向到/login If i disable formLogin如果我禁用formLogin

.formLogin().disable()

then i also lose my /login POST endpoint, and springs default authentication process.然后我也丢失了我的/login POST 端点,并启动了默认身份验证过程。

Is there a way to keep to /login POST endpoint but remove the login page and let my frond-end client to redirect.有没有办法保留/login POST 端点但删除login页面并让我的前端客户端重定向。

Thanks for help!感谢帮助!

I agree with Toerktumlare 100%.我同意 Toerktumlare 100%。 You really should use the default implementations that Spring Security Framework offers and if you really need to add additional configuration that cannot be passed by values, then you should extend the original Spring Security Class.您确实应该使用 Spring Security Framework 提供的默认实现,如果您确实需要添加无法通过值传递的额外配置,那么您应该扩展原始 Spring Security Class。

In this case, you just have to specify the endpoint where your login page is:在这种情况下,您只需指定登录页面所在的端点:

.formLogin()
.loginPage("/yourLoginPage.html")
.loginProcessingUrl("/doLgin")
.defaultSuccessUrl("/yourHomePahe.html", true)

When you use the GET method, you will get html page.当您使用GET方法时,您将获得 html 页面。 As I understand you need POST API call, Post API call to get JWT access_tocken and refresh tocken据我了解,您需要POST API 调用、 Post API 调用以获取 JWT access_token 并刷新令牌

you can use "/login" as API in post call to send a username and password;您可以在post call 中使用“/login”作为 API 来发送用户名和密码; in response, you will get a token or anything that you send after successful authentication.作为回应,您将获得一个令牌或您在成功验证后发送的任何内容。

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

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