【问题标题】:How to change endpoint address for generate JWT token如何更改端点地址以生成 JWT 令牌
【发布时间】:2018-04-08 23:16:36
【问题描述】:

默认情况下,spring boot web security 使用/login 端点登录并生成令牌。但是我需要/user/login 端点来做到这一点。我的 WebSecurityConfig 看起来像这样:

protected void configure(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable().authorizeRequests()
            .antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll()
            .anyRequest().authenticated()
            .and()
            .addFilter(new JWTAuthenticationFilter(authenticationManager()))
            .addFilter(new JWTAuthorizationFilter(authenticationManager()))
            // this disables session creation on Spring Security
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

我想知道如何更改端点地址以生成 JWT 令牌?提前致谢!

【问题讨论】:

    标签: java spring-boot spring-security authorization jwt


    【解决方案1】:

    您可以像这样更改 .loginPage() 参数;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/", "/home", "/about").permitAll()
                .antMatchers("/admin/**").hasAnyRole("ADMIN")
                .antMatchers("/user/**").hasAnyRole("USER")
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/user/login")
                .permitAll()
                .and()
                .logout()
                .permitAll()
                .and()
                .exceptionHandling().accessDeniedHandler(accessDeniedHandler);
    }
    

    【讨论】:

    • 感谢您的回答,但这并不能解决问题,因为 formLogin() 会生成登录页面,我想避免这种情况。我只想将默认端点 '/login' 更改为 '/user/login'
    • 对不起我的错误。看看这个:link
    【解决方案2】:

    我做了一些调查,但这是不可能的,因为要生成令牌,我使用自己的过滤器 (JWTAuthenticationFilter)。该过滤器从 UsernamePasswordAuthenticationFilter 扩展而来,并且在构造函数中有一个硬编码端点:

    public UsernamePasswordAuthenticationFilter() {
      super(new AntPathRequestMatcher("/login", "POST"));
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 2021-07-29
      • 2019-10-13
      • 2023-04-09
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      相关资源
      最近更新 更多