首页 > 解决方案 > Spring Security 的默认登录表单可以与 JWT 一起使用吗?

问题描述

我创建了一个 REST API 后端,通过 JWT 对用户进行身份验证和授权。我还向我的应用程序添加了通过电子邮件注册用户的功能。因此,在注册期间,我创建了一个发送给用户的链接,当他点击它时,他被重定向到一个网页,通知他他的帐户已被激活。我现在要做的是合并默认的 spring 安全登录表单,以保护确认链接不被那些可能错误地收到电子邮件的人影响。因此,每当他想访问此链接时,他都需要先登录。

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.cors().and().csrf().disable()
            .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .headers().frameOptions().sameOrigin()
            .and()
            .authorizeRequests()
            .antMatchers(
                    "/",
                    "/favicon.ico",
                    "/**/*.png",
                    "/**/*.gif",
                    "/**/*.svg",
                    "/**/*.jpg",
                    "/**/*.html",
                    "/**/*.css",
                    "/**/*.js"
            )
            .permitAll()
            .antMatchers(SIGN_UP_URLS).permitAll()
            .antMatchers(H2_URL).permitAll()
            .anyRequest().authenticated();

    http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

  }

标签: spring-bootspring-securityjwt

解决方案


推荐阅读