我在地址栏中输入的任何链接都会将我重定向到登录页面。我该怎么防止呢?
例如如果我加上http://localhost:8080/asdasdsa
我的安全配置:
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/**");
}
}
这里有一个解决办法!
// Required to provide UserDetailsService for "remember functionality"
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("{noop}password").roles("USER");
}
将上述代码段添加到安全配置类中,添加pom中定义的相同用户名和密码。这里也是xml。(不移除“{noop}”部分)
所以下次登录时,不要忘记点击“记住我”复选框,否则问题会不断重复。
有关更多详细信息,请参阅本文。
尝试使用127.0.0.1
而不是localhost
。
解释
您的浏览器没有为localhost
设置JSESSIONID
cookie(请参阅此SO问题),因此在成功登录后重定向时,下一个请求看起来没有通过服务器身份验证。
如果你使用idea,你打开浏览器点击服务面板,你可以在你的application-dev.yml
中添加这个配置来解决这个问题:
server:
address: 127.0.0.1
您配置所有其他URL都必须经过身份验证,请参阅Spring Security参考:
授权请求
我们的示例只要求用户进行身份验证,并且对应用程序中的每个URL都这样做。我们可以通过向http添加多个子元素来指定URL的自定义要求。authorizeRequests()
方法。例如:
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests() 1
.antMatchers("/resources/**", "/signup", "/about").permitAll() 2
.antMatchers("/admin/**").hasRole("ADMIN") 3
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") 4
.anyRequest().authenticated() 5
.and()
// ...
.formLogin();
}
1 http有多个子项。authorizeRequests()方法按照声明的顺序考虑每个匹配器。
2我们指定了任何用户都可以访问的多个URL模式。具体来说,如果URL以“/Resources/”开头、等于“/signup”或等于“/about”,则任何用户都可以访问请求。
3任何以“/admin/”开头的URL将仅限于具有“role_admin”角色的用户。您会注意到,由于我们调用的是hasRole方法,所以不需要指定“ROLE_u2;”前缀。
4任何以“/db/”开头的URL都要求用户同时拥有“ROLE_ADMIN”和“ROLE_DBA”。您会注意到,因为我们使用的是hasRole表达式,所以不需要指定“ROLE_u2;”前缀。
5任何尚未匹配的URL只需要对用户进行身份验证
我已经在我的应用程序上配置了Spring Security性,如下所示,但每当我们调用RequestMapping POST方法的URL时,它总是将我们重定向回登录页面(请注意,我们是以管理员身份登录的)。我错过什么了吗?
记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano
我现在一直在努力使用我的登录页面来让组件呈现Loggedin组件。我的前端是Reactjs,后端是NodeJS。我对nodejs、expression和react都是新手。 在loginform组件上,我使用fetch进行了一次post,它将用户名和密码传递给后端的相应endpoint。没问题。在后端,它读取我存储用户(不使用任何数据库)的jsonfile来查找匹配项,如果用户名和密码都匹配,则它
我正在将Shiro集成到我的dropwizard WebApp中。我已经说到这一点了 null 并且每次访问/admin都会导致错误302。我不知道还有什么相关的来回答这个问题。我已经探讨了其他几个“Shiro不断将我重定向到登录”的问题,但大多数都直接提到了带有web.xmls的Jetty,Dropwizard不使用它,我也不知道应用了什么过滤器。我的同事用同样的方法让shiro处理他的drop
我在Laravel框架中的HomeController.php中有一段代码,但是我无法访问它重定向到登录页面的索引/主页。我犯了什么大错?
问题内容: 我有一个网站,有一个用户系统。我想将wordpress的用户系统集成到该网站的系统中,但是我仍然想使用该网站的注册/登录页面。我不希望任何人都能使用Wordpress的登录或注册表格进行登录或注册。相反,当他们尝试访问Wordpress中的登录/注册页面时,我希望这些页面将它们重定向到我自己的登录/注册页面。 有什么办法吗?我已经尝试过Google,但是我所能找到的只是在用户登录或注册