Java 如何更改生成JWT令牌的端点地址
默认情况下,spring boot web安全性使用Java 如何更改生成JWT令牌的端点地址,java,spring-boot,spring-security,authorization,jwt,Java,Spring Boot,Spring Security,Authorization,Jwt,默认情况下,spring boot web安全性使用/login端点登录并生成令牌。然而,我需要/user/login端点来完成这项工作。我的Web安全配置如下所示: protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable().authorizeRequests() .antMatchers(HttpMethod.POST, SI
/login
端点登录并生成令牌。然而,我需要/user/login
端点来完成这项工作。我的Web安全配置如下所示:
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令牌的端点地址?提前谢谢 您可以像这样更改.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);
}
您可以像这样更改.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);
}
我做了一些调查,但不可能做到这一点,因为要生成令牌,我使用自己的过滤器(JWTAuthenticationFilter)。该筛选器从UsernamePasswordAuthenticationFilter扩展而来,构造函数中有一个硬编码端点:
public UsernamePasswordAuthenticationFilter() {
super(new AntPathRequestMatcher("/login", "POST"));
}
我做了一些调查,但不可能做到这一点,因为要生成令牌,我使用自己的过滤器(JWTAuthenticationFilter)。该筛选器从UsernamePasswordAuthenticationFilter扩展而来,构造函数中有一个硬编码端点:
public UsernamePasswordAuthenticationFilter() {
super(new AntPathRequestMatcher("/login", "POST"));
}
谢谢您的回答,但这并不能解决问题,因为formLogin()生成登录页面,我希望避免这种情况。我只想将默认端点'/login'更改为'/user/login'对不起,我的错误。检查:感谢您的回答,但这并不能解决问题,因为formLogin()生成登录页面,我希望避免这种情况。我只想将默认端点'/login'更改为'/user/login'对不起,我的错误。看看这个: