我是Spring Boot的新手,我有一个使用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/**");
}
}
参考方案
您需要什么行为?基本上,有两种选择:重定向到某个静态的静态已知位置(例如/index
),或重定向到最初请求的页面。两者都需要配置AuthenticationSuccessHandler
。您还可以使用/扩展现有的身份验证处理程序之一来完成一些基本任务。例如,请注意如何使用SimpleUrlAuthenticationSuccessHandler
重定向到最初请求的页面:
XML安全配置:
<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
:
public class AuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
// Very simple (most probably broken) check if the user is ADMIN or USER
if (authentication.getAuthorities().stream().filter(a -> a.getAuthority().equals("USER")).findAny() != null){
redirectStrategy.sendRedirect(request, response, "/profile.html");
} else {
redirectStrategy.sendRedirect(request, response, "/admin.html");
}
clearAuthenticationAttributes(request);
}
}
Java-搜索字符串数组中的字符串 - java在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…
Spring中的应用程序上下文有什么作用? - java我昨天问了一个问题(Using Spring in standalone apps),有关如何在独立应用程序中使用Spring。由此得知,您只创建一次应用程序上下文对象。因此,现在的问题是(即使在评论中得到了部分回答)创建应用程序上下文时会发生什么?当您说时,Spring是否会创建这些豆子并将它们连接在一起new ClassPathXmlApplicatio…
注解中的Spring属性值 - java如何获取注释内的属性值。例如我有一个注释@GetMyValue(value1="Val1",intVal=10) 现在,我希望“ Val1”和10来自属性文件。我试过了@GetMyValue(value1="${test.value}",intVal="${test.int.value}") 这不起…
Spring Boot如何在POST之后返回响应 - java我想创建一个新客户并在创建客户后返回客户编号。客户编号必须是从50000开始的自动递增的唯一编号。到目前为止,我已经成功创建了一个客户,但是我不确定应该如何生成客户编号,将其保存到数据库中,并在触发POST时将其作为成功消息显示给用户。json下面是所需的响应;{ "customerNumber": "50002", …
春天的多属性文件 - java我在spring中加载属性文件: <context:property-placeholder location="classpath:foo.properties"/> 但是,如果我尝试在另一个上下文文件中加载另一个文件,则会出现错误。 java大神给出的解决方案 如果您需要覆盖属性,则可以执行以下操作:<context…