如何在Spring Security中更改登录URL

Sam*_*ıcı 0 configuration spring login spring-security spring-boot

我创建了一个提供用户身份验证的API,它的登录操作由Spring Security 在默认的'/ login'路径上处理.

我想将此路径更改为"api/v1/login".

这是我的安全配置:

http.cors().and().csrf().disable()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests()
            .antMatchers("/h2-console/**/**").permitAll()
            .antMatchers(HttpMethod.POST,"/user/register").permitAll()
            .antMatchers("/user/activate").permitAll()
            .antMatchers("/user/reset-password").permitAll()
            .antMatchers("/user/reset-password").permitAll()
            .antMatchers("/admin/user").hasRole("ADMIN")
            .antMatchers("/roles").permitAll()
            .antMatchers("/user/**").hasRole("USER")
            .and()
            .formLogin().loginProcessingUrl("/api/v1/login")
            .and()
            .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
            .and()
            .addFilterBefore(new ExceptionHandlerFilter(), UsernamePasswordAuthenticationFilter.class)
            .addFilter(new JwtAuthenticationFilter(authenticationManager()))
            .addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtUserDetailService));
Run Code Online (Sandbox Code Playgroud)

我添加了这一行来改变它:

.formLogin().loginProcessingUrl("/api/v1/login")
Run Code Online (Sandbox Code Playgroud)

但它仍然在'/ login'路径下工作.

"/ api/v1/login"返回404.

有没有办法改变它?

Spring Boot版本:2.0.0.RELEASE

kak*_*ali 9

该函数.loginProcessingUrl("/api/v1/login")指定用于验证凭据的URL,用于验证用户名和密码的URL.

它只会覆盖的URL /api/v1/loginPOST类型,而不是GET

它不会将请求传递给Spring MVC和您的控制器

有关其他自定义,您可以查看 FormLoginConfigurer

更新v1

您还可以检查您的网址/api/**是否全部安全?

如果是,则尝试从中删除安全性/api/v1/login,并将permitAll()配置添加到此URL

查看这篇文章 - /sf/answers/1923020571/.如果它有助于您的方案

更新v2 - 这在这里有所帮助

你没有正确地发送用户名和密码,为了工作,请参考下面的内容,在它出现的时候BadCredentialsException.我在应用程序上启用了调试,并且能够解决这个问题.

你需要将参数发布到网址 - http:// localhost:8080/api/v1/login如下(还附上了图片,邮递员的截图): -

标题: Content-Type=application/x-www-form-urlencoded

键值对中的参数(不是json格式,请参考图片):

username=player3
password=pass3
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

上面你可以从index.html响应如下: -

<a href="http://localhost:8080/other.html">test static resource</a>
Run Code Online (Sandbox Code Playgroud)

您还需要自定义.

对于发送用户名和密码的JSON请求,可以轻松完成的更改将是: -

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

    http.cors().and().csrf().disable()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
        .authorizeRequests()
        .antMatchers("/h2-console/**/**").permitAll()
        .antMatchers(HttpMethod.POST,"/user/register").permitAll()
        .antMatchers("/user/activate").permitAll()
        .antMatchers("/user/reset-password").permitAll()
        .antMatchers("/user/reset-password").permitAll()
        .antMatchers("/admin/user").hasRole("ADMIN")
        .antMatchers("/roles").permitAll()
        .antMatchers("/user/**").hasRole("USER")
        //.and()
        //.formLogin().loginProcessingUrl("/api/v1/login") // not required any more
        .and()
        .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
        .and()
        .addFilterBefore(new ExceptionHandlerFilter(), UsernamePasswordAuthenticationFilter.class)
        .addFilter(jwtAuthorizationFilter())
        .addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtUserDetailService));

    http.headers().frameOptions().disable(); // its require for h2-console

}

public JwtAuthenticationFilter jwtAuthorizationFilter() throws Exception {
    JwtAuthenticationFilter jwtAuthenticationFilter = new JwtAuthenticationFilter(authenticationManager());
    jwtAuthenticationFilter.setFilterProcessesUrl("/api/v1/login");
    return jwtAuthenticationFilter;
}
Run Code Online (Sandbox Code Playgroud)

并且.formLogin().loginProcessingUrl("/api/v1/login")不再需要代码了

此外,您需要将成功和失败的URL添加到应用程序,并使您的登录URL获取基于json的用户凭据,您需要跟进并需要做更多的东西,一些有用的参考 - https:/ /stackoverflow.com/a/19501060/2600196


小智 6

您正在扩展 \xc2\xa0org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter \xc2\xa0 ,它本身扩展了 org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter。在最后一个类中,有一个名为\xc2\xa0 的setter

\n\n
\n

设置过滤进程 URL

\n
\n\n

\xc2\xa0which 的目的就是这样做:

\n\n
\n

设置过滤进程 URL

\n\n

公共无效\xc2\xa0setFilterProcessesUrl(字符串filterProcessesUrl)

\n\n

设置确定是否需要身份验证的 URL

\n\n

参数:filterProcessesUrl

\n
\n\n

这个\xc2\xa0 是该 javadoc 部分的链接

\n\n

所以在你的\xc2\xa0WebSecurityConfigurerAdapter\xc2\xa0你可以这样做:

\n\n
@Bean \npublic JWTAuthenticationFilter getJWTAuthenticationFilter() { \n    final JWTAuthenticationFilter filter = new JWTAuthenticationFilter(authenticationManager()); \n    filter.setFilterProcessesUrl("/api/auth/login"); \n    return filter; \n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后在同一个类中的\xc2\xa0configure\xc2\xa0方法中引用它而不是创建新实例:

\n\n
.addFilter(getJWTAuthenticationFilter\n
Run Code Online (Sandbox Code Playgroud)\n