gpt4 book ai didi

java - 单页应用如何配置Spring Security?

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:30 24 4
gpt4 key购买 nike

我在为单页应用程序配置 Spring Security 时遇到了问题。

所以,默认配置看起来像

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
@Qualifier("customUserDetailsService")
UserDetailsService userDetailsService;

@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/list").permitAll()
.antMatchers("/admin/**").access("hasRole('ADMIN')")
.and().formLogin().loginPage("/login").permitAll()
.usernameParameter("ssoId").passwordParameter("password")
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/Access_Denied");
}

@Bean(name="authenticationManager")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}


}

从 Login().loginPage("/login") 方法的文档中可以看出它用于重定向到登录页面。对于单页,此配置不相关。我应该如何为单页应用程序配置 spring?我的意思是如何在 Controller 和配置文件中配置登录、注销。

最佳答案

Spring Lemon可以是一个完整的例子,但让我总结以下内容。

默认情况下,当用户成功登录时,Spring Security 会将他重定向到主页。当登录失败或成功注销后,用户将被重定向回登录页面。此外,在尝试访问用户没有足够权限的 URL 时,他将被重定向到登录页面。

如您所说,此行为不适合单页应用程序。您的 API 应该随用户数据一起发送 200 响应或 4xx 响应。这可以通过提供您自己的处理程序来完成,如下所示:

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
...
.successHandler(your authentication success handler object)
.failureHandler(your authentication failure handler object)
.and()
.logout()
...
.logoutSuccessHandler(your logout success handler object)
.and()
.exceptionHandling()
.authenticationEntryPoint(new Http403ForbiddenEntryPoint())
...
}

您会在 Internet 上找到许多关于如何编写这些处理程序类的示例。例如,在 spring-lemon项目,这些编码如下。

Authentication Success Handler

@Component
public class AuthenticationSuccessHandler
extends SimpleUrlAuthenticationSuccessHandler {

@Autowired
private ObjectMapper objectMapper;

@Autowired
private LemonService<?,?> lemonService;

@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response,
Authentication authentication)
throws IOException, ServletException {

response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);

AbstractUser<?,?> currentUser = lemonService.userForClient();

response.getOutputStream().print(
objectMapper.writeValueAsString(currentUser));

clearAuthenticationAttributes(request);
}
}

总而言之,它返回一个 200 响应,响应数据中包含 JSON 化的当前用户。

身份验证失败处理程序

事实上,不需要为身份验证失败处理程序编写类 - Spring 提供的 SimpleUrlAuthenticationFailureHandler,如果在没有任何参数的情况下实例化,可以按预期工作。

注销成功处理程序

public class LemonLogoutSuccessHandler
implements LogoutSuccessHandler {

@Override
public void onLogoutSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {

response.setStatus(HttpServletResponse.SC_OK);
}
}

详细例子引用Spring Lemon的LemonWebSecurityConfig它的各种模块的安全包中的类和其他类可能会有所帮助。

关于java - 单页应用如何配置Spring Security?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32915057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com