我是Spring的新手,我有一个小应用程序使用Spring Boot和Spring Security。成功登录后,页面将重定向到/ login。我不知道如何解决它。
成功登录后:
这是安全配置:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/", "/login").permitAll()//设置SpringSecurity对"/"和"/login"路径不拦截
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")//设置Spring Security的登录页面访问路径为/login
.defaultSuccessUrl("/chat")//登录成功后转向/chat路径
.permitAll()
.and()
.logout()
.permitAll();
}
/**
* 在内存中分别配置两个用户xin.luo和king.luo,密码和用户名一致,角色是USER
* @param auth
* @throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("xin").password("xin").roles("USER")
.and()
.withUser("king").password("king").roles("USER");
}
/**
* /resources/static/目录下的静态资源文件,Spring Security不拦截
* @param web
* @throws Exception
*/
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/static/**");
}
}
答案 0 :(得分:0)
您需要什么行为?基本上,有两种选择:重定向到某些静态静态,众所周知的位置,如 <ul id="sortList">
<li id="one"><a href='#'>Ratings</a></li>
<li id="2"><a href=#>Reviews</a></li>
<li id="3"><a href=#>Type</a></li>
</ul>
<h2 id="anchorInfo">hello</h2>
<h2 id="clickInfo">hello</h2>
$('#sortList li').click( function(event){
event.preventDefault();
let whatHref = $(event.target).prop('href');
let whatId = $(event.target).prop('id');
document.getElementById('anchorInfo').innerHTML = whatHref;
document.getElementById('clickInfo').innerHTML = whatId;
})
,或重定向到最初请求的页面。两者都需要配置AuthenticationSuccessHandler
。您还可以使用/扩展其中一个现有的auth处理程序来完成一些基本任务。例如,请注意SimpleUrlAuthenticationSuccessHandler
如何用于重定向到最初请求的页面:
XML Secutiry config:
/index
示例<http use-expressions="true">
<intercept-url pattern="/login*" access="permitAll"/>
<intercept-url pattern="/**" access="isAuthenticated()"/>
<form-login
...
authentication-success-handler-ref="authenticationSuccessHandler"
authentication-success-handler-ref="refererAuthenticationSuccessHandler"
...
/>
<logout/>
</http>
<!-- Route users to their profiles and admins to the admin console: -->
<beans:bean id="authenticationSuccessHandler" class="a.b.c.AuthenticationSuccessHandler"/>
<!-- Route to the originally requested page -->
<beans:bean id="refererAuthenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
<property name="useReferer" value="true"/>
</beans:bean>
:
AuthenticationSuccessHandler
答案 1 :(得分:0)
还有另一种可能性。未设置Cookie,并且以下发送的所有请求均被视为没有会话ID的第一个请求。
如果您使用的是Google chrome,并且使用本地主机地址在本地计算机上测试了该应用程序,则可能未设置Cookie。您可以在此处查看更多详细信息:Chrome localhost cookie not being set
您可以尝试使用127.0.0.1进行测试。