Spring Security不断将我重定向到登录页面

我在地址栏中输入的任何链接都会使我重定向到登录页面。我该如何预防?

例如,如果我添加http:// localhost:8080 / asdasdsa

,它将重定向我到 http:// localhost:8080 / account /

login,因此,如果我在http:// localhost:8080

/之后添加任何内容,我将被重定向帐户/登录视图。

我的安全性配置:

package com.example.configuration;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Configuration;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;

import org.springframework.security.config.annotation.web.builders.WebSecurity;

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@Configuration

@EnableWebSecurity

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired

private BCryptPasswordEncoder bCryptPasswordEncoder;

@Autowired

private DataSource dataSource;

@Value("${spring.queries.users-query}")

private String usersQuery;

@Value("${spring.queries.roles-query}")

private String rolesQuery;

@Override

protected void configure(AuthenticationManagerBuilder auth)

throws Exception {

auth

.jdbcAuthentication()

.usersByUsernameQuery(usersQuery)

.authoritiesByUsernameQuery(rolesQuery)

.dataSource(dataSource)

.passwordEncoder(bCryptPasswordEncoder);

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http

.authorizeRequests()

.antMatchers("/").permitAll()

.antMatchers("/index").permitAll()

.antMatchers("/other/other").permitAll()

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

.antMatchers("/account/registration").permitAll()

.antMatchers("/account/admin/**").hasAuthority("ADMIN")

.anyRequest().authenticated()

.and()

.csrf().disable()

.formLogin()

.loginPage("/account/login")

.failureUrl("/account/login?error=true")

.defaultSuccessUrl("/account/admin/")

.usernameParameter("email")

.passwordParameter("password")

.and()

.logout().permitAll()

.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))

.logoutSuccessUrl("/")

.and()

.exceptionHandling()

.accessDeniedPage("/access-denied");

}

@Override

public void configure(WebSecurity web) throws Exception {

web

.ignoring()

.antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**", "/img/**");

}

}

回答:

您配置了所有其他URL必须通过身份验证,请参阅Spring Security

Reference:

我们的示例仅要求对用户进行身份验证,并且对应用程序中的每个URL都进行了身份验证。我们可以通过向我们的http.authorizeRequests()方法添加多个子级来为URL指定自定义要求。例如:

protected void configure(HttpSecurity http) throws Exception {

http

.authorizeRequests()

1

.antMatchers(“/resources/”).hasRole(“ADMIN”)

3

.antMatchers(“/db/**”).access(“hasRole(‘ADMIN’) and

hasRole(‘DBA’)”) 4

.anyRequest().authenticated()

5

.and()

// …

.formLogin();

}

http.authorizeRequests()方法有多个子级,每个匹配器都按声明顺序考虑。

我们指定了任何用户都可以访问的多个URL模式。具体来说,如果URL以“ / resources /”开头,等于“ / signup”或等于“

/ about”,则任何用户都可以访问请求。

任何以“ / admin /”开头的URL都将限于角色为“

ROLE_ADMIN”的用户。您将注意到,由于我们正在调用hasRole方法,因此无需指定“ ROLE_”前缀。

任何以“ / db /”开头的URL都要求用户同时具有“ ROLE_ADMIN”和“

ROLE_DBA”。您将注意到,由于我们使用的是hasRole表达式,因此无需指定“ ROLE_”前缀。

任何尚未匹配的URL仅要求对用户进行身份验证

以上是 Spring Security不断将我重定向到登录页面 的全部内容, 来源链接: utcz.com/qa/432438.html

回到顶部