当前位置: 首页 > 知识库问答 >
问题:

在spring security中更改登录服务URL

花阳秋
2023-03-14

嗨,我已经用JWT过滤器在我的Spring boot web应用程序中实现了Spring Security性。但默认身份验证发生在urlhttp://localhost:8080/login。如何将/login更改为我需要的url,如/rest/auth/login

我的WebSecurity类是

@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;

public WebSecurity( UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder )
{
    this.userDetailsService = userDetailsService;
    this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}

@Override
protected void configure( HttpSecurity http ) throws Exception
{
    http.cors().and().csrf().disable().authorizeRequests().antMatchers(HttpMethod.POST, "/rest/auth/**").permitAll()
            .antMatchers("/static/*").permitAll().antMatchers("/").permitAll()
            /* .anyRequest().authenticated() */.and()
            .addFilter(new JWTAuthenticationFilter(authenticationManager()))
            .addFilter(new JWTAuthorizationFilter(authenticationManager()));
}

@Override
public void configure( AuthenticationManagerBuilder auth ) throws Exception
{
    auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}

@Override
public void configure( org.springframework.security.config.annotation.web.builders.WebSecurity web )
        throws Exception
{

    web.ignoring().antMatchers("/static/**");
}

@Bean
CorsConfigurationSource corsConfigurationSource()
{
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
    return source;
}
}

我在静态目录下的资源文件夹中有一个登录页面。Spring security的工作方式是,当用户从表单发送usernamepassword时,客户端必须将这些凭据发送到服务器中的/login路径,以便Spring security验证这些凭据并创建令牌。但我想将默认路径/login更改为/rest/auth/login

共有1个答案

龚镜
2023-03-14

您需要调整websecurityconfig.javajwtauthenticationfilter

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

    http.csrf().disable()

            .authorizeRequests()

            .antMatchers("/rest/noauth/**").permitAll()

            .antMatchers("/rest/login").permitAll()

            .antMatchers("/rest/logout").permitAll()

            .antMatchers("/src/**").permitAll()

            .antMatchers("/v2/api-docs/**", "/configuration/ui/**", "/swagger-resources/**",
                    "/configuration/security/**", "/swagger-ui.html/**", "/webjars/**")
            .permitAll()

            .anyRequest().authenticated()

            .and()

            .logout().addLogoutHandler(logoutHandler).logoutSuccessHandler(logoutSuccessHandler)
            .logoutUrl("/rest/logout")

            .and()

            .addFilterBefore(
                    new JWTAuthenticationFilter("/rest/login",
                    UsernamePasswordAuthenticationFilter.class)

            .addFilterBefore(new JWTAuthorizationFilter(authenticationManager(), authTokenModelRepository),
                    UsernamePasswordAuthenticationFilter.class);

    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

}

并使jwtauthenticationfilter扩展abstractauthenticationprocessingfilter,后者具有一个构造函数,该构造函数将filterprocessingurl和我传递的/rest/login作为参数。

public class JWTAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(JWTAuthenticationFilter.class);

private AuthenticationManager authenticationManager;
private TokenService tokenService;
private UserModel credentials;

private RefreshTokenService refreshTokenService;
private AuthTokenModelRepository authTokenModelRepository;
private UserModelRepository userModelRepository;

public JWTAuthenticationFilter( String loginUrl, AuthenticationManager authenticationManager,
        TokenService tokenService, RefreshTokenService refreshTokenService,
        AuthTokenModelRepository authTokenModelRepository, UserModelRepository userModelRepository )
{
    super(new AntPathRequestMatcher(loginUrl));

}

完成上述配置后,将为请求/rest/login执行jWTauthenticationfilter

 类似资料:
  • 使用以下命令登录 ftp服务器: lftp ftp://用户名[:密码]@服务器地址[:端口] #标准方式,推荐 lftp 用户名[:密码]@服务器地址[:端口] lftp 服务器地址 [-p 端口] -u 用户名[,密码] lftp 服务器地址[:端口] -u 用户名[,密码] 如果不指定端口,默认 21 如果不在命令中使用明文输入密码,连接时会询问密码(推荐) 可以使用“书签”收藏服务器

  • springsecurity oauth2.0 谁做过记录登录日志?监听事件好像没法区分是什么原因失败的、比如client错误还是用户名错误

  • 以前版本的改造使用RestaAdapter,并提供了启用日志的功能。为什么在改型2.0中删除了该功能? 要启用日志,我必须。。 这是唯一的解决办法吗?以前的规定非常方便...

  • 您好,我正在使用Laravel5.2版本。我通过composer安装了laravel项目。之后,我使用命令“phpartisanmake:auth”创建auth。创建身份验证路由后生成,例如“http://localhost:8000/login“.现在我不想要此路线,我想设置不同的路线,例如”http://localhost:8000/super/admin“那么,我该如何更改”http://l

  • 本文向大家介绍Django 登录到Syslog服务,包括了Django 登录到Syslog服务的使用技巧和注意事项,需要的朋友参考一下 示例 可以将Django配置为将日志输出到本地或远程syslog服务。此配置使用python内置的SysLogHandler。            

  • 我正在尝试在登录后更改vaadin中的JSESSIONID。 这是我的密码 JSESSIONID正在成功更改,但在登录到我的仪表板后,im getting SESSION EXPIRED框。我怎么才能在瓦丁解决这件事。