- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据新要求,我创建了自定义 UsernamePasswordAuthenticationFilter 以从登录页面捕获其他参数。正如预期的那样,我的配置工作正常。我能够在过滤器中检索其他参数并保存到 session 。但是在将我的自定义过滤器添加到配置后, session 管理不起作用。以前我通过将最大 session 值设置为 1 只允许每个用户一个 session 。它现在不起作用,应用程序允许同一用户多次登录。我确定只有在将自定义 UsernamePasswordAuthenticationFilter 集成到我的配置后才会发生这种情况。下面是我的 spring 安全配置。
http.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/login.html")
.usernameParameter("username")
.passwordParameter("password")
.and()
.logout()
.logoutSuccessUrl("/login.html")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout.html"))
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.and()
.sessionManagement()
.maximumSessions(1)
.expiredUrl("/multiplesessions.html")
.sessionRegistry(getSessionRegistry());
http.addFilterBefore(customUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
@Bean
public SessionRegistry getSessionRegistry() {
return new SessionRegistryImpl();
}
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(dsnyUserDetailsService);
provider.setPasswordEncoder(passwordEncoder());
auth.authenticationProvider(provider);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new StandardPasswordEncoder();
}
@Bean(name = "myAuthenticationManager")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
DsnyUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter() throws Exception {
DsnyUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter = new DsnyUsernamePasswordAuthenticationFilter();
customUsernamePasswordAuthenticationFilter.setAuthenticationManager(authenticationManagerBean());
customUsernamePasswordAuthenticationFilter.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login.html", "POST"));
return customUsernamePasswordAuthenticationFilter;
}
最佳答案
我通过添加自定义 ConcurrentSessionFilter 解决了这个问题。如果有人想要,这是代码。
http.sessionManagement().sessionAuthenticationStrategy(concurrentSession());
http.addFilterBefore(concurrentSessionFilter(), ConcurrentSessionFilter.class);
@Bean
public CompositeSessionAuthenticationStrategy concurrentSession() {
ConcurrentSessionControlAuthenticationStrategy concurrentAuthenticationStrategy = new ConcurrentSessionControlAuthenticationStrategy(getSessionRegistry());
concurrentAuthenticationStrategy.setMaximumSessions(1);
//concurrentAuthenticationStrategy.setExceptionIfMaximumExceeded(true);
List<SessionAuthenticationStrategy> delegateStrategies = new ArrayList<SessionAuthenticationStrategy>();
delegateStrategies.add(concurrentAuthenticationStrategy);
delegateStrategies.add(new SessionFixationProtectionStrategy());
delegateStrategies.add(new RegisterSessionAuthenticationStrategy(getSessionRegistry()));
CompositeSessionAuthenticationStrategy authenticationStrategy = new CompositeSessionAuthenticationStrategy(delegateStrategies);
return authenticationStrategy;
}
@Bean
ConcurrentSessionFilter concurrentSessionFilter() {
CustomSessionInformationExpiredStrategy redirectStrategy = new CustomSessionInformationExpiredStrategy("/pub/multiplesessions.html");
CustomConcurrentSessionFilter concurrentSessionFilter = new CustomConcurrentSessionFilter(getSessionRegistry(), redirectStrategy);
return concurrentSessionFilter;
}
public class CustomSessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {
private Logger log = Logger.getLogger(this.getClass().getName());
private String expiredUrl = "";
public CustomSessionInformationExpiredStrategy(String expiredUrl) {
this.expiredUrl = expiredUrl;
}
@Override
public void onExpiredSessionDetected(SessionInformationExpiredEvent sessionInformationExpiredEvent) throws IOException, ServletException {
log.info("Redirecting to session expired page");
HttpServletRequest request = sessionInformationExpiredEvent.getRequest();
HttpServletResponse response = sessionInformationExpiredEvent.getResponse();
request.getSession();// creates a new session
response.sendRedirect(request.getContextPath() + expiredUrl);
}
}
public class CustomConcurrentSessionFilter extends ConcurrentSessionFilter {
public CustomConcurrentSessionFilter(SessionRegistry sessionRegistry) {
super(sessionRegistry);
}
public CustomConcurrentSessionFilter(SessionRegistry sessionRegistry, SessionInformationExpiredStrategy sessionInformationExpiredStrategy) {
super(sessionRegistry, sessionInformationExpiredStrategy);
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
super.doFilter(req, res, chain);
}
}
关于spring-security - 带有自定义 UsernamePasswordAuthenticationFilter 的 Spring 安全并发控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44432885/
我很难配置我的 Spring 安全性。问题是,每当我通过自定义 UsernamePasswordAuthenticationFilter 进行身份验证时,我的身份验证过滤器总是会跳过成功和失败处理程序
我正在延长 UsernamePasswordAuthenticationFilter这样我就可以添加自定义字段以将它们保存到 session 中。 public class AuthFilter ex
我目前正在开发一个需要高度可扩展的 REST 应用程序。根据我对负载平衡的了解,应该将实际应用程序分成独立的部分。到目前为止,我已将创建帐户和登录与实际应用程序分开。我关注了this实现 JWT 的教
我正在尝试扩展 UsernamePasswordAuthenticationFilter,如本教程所示:http://blog.awnry.com/post/16183749439/two-facto
我正在创建一个自定义过滤器 UsernamePasswordAuthenticationFilter 来处理身份验证过程。基本上,当用户通过 REST/login 执行登录 http post 时,自
我目前正在开发一个需要高度可扩展的 REST 应用程序。根据我对负载平衡的了解,应该将实际应用程序分成独立的部分。到目前为止,我已将创建帐户和登录与实际应用程序分开。我关注了this实现 JWT 的教
我想在UsernamePasswordAuthenticationFilter之前调用一个方法.attemptAuthentication() 被调用(并且在应用程序中处理任何其他请求之前)。 我尝试
使用 XML 配置会像 . 没有 XML 会像: httpSecurity.addFilter(new SCAAthenticationFilter()) spring好像没有替换UsernameP
我已经实现了自己的LowerCaseUsernamePasswordAuthenticationFilter这只是 UsernamePasswordAuthenticationFilter 的子类.
根据新要求,我创建了自定义 UsernamePasswordAuthenticationFilter 以从登录页面捕获其他参数。正如预期的那样,我的配置工作正常。我能够在过滤器中检索其他参数并保存到
我尝试使用 Spring Security 生成 JWT token ,但我不知道如何正确执行此操作(使用最佳实践)。 我是否应该在某处“拦截” UsernamePasswordAuthenticat
这个问题在这里已经有了答案: Why is my Spring @Autowired field null? (21 个回答) 关闭 5 年前。 我是 spring boot 的新手,我正在尝试在我
我正在关注 this auth0's tutorial使用 JWT 保护我的应用程序。 我最终得到了以下 WebSecurity 配置: @EnableWebSecurity @AllArgsCons
我想在 Spring-MVC 中为登录和注销页面创建具有不同角色的不同用户名。 我已经从下面的链接进行了研究,我使用的是 spring 4.2.5: http://www.raistudies.com
有人知道我可以在 spring security.xml 中同时使用 and 吗?目前,我在 xml 中的标签下使用 UsernamePasswordAuthenticationFilter 。但是当
我将 spring Boot MVC Security 与 JPA Hibernate 和 Thymeleaf 模板引擎一起使用。我正在尝试使用一些已存储在数据库 (MySQL) 中的用户创建用户登录
我是一名优秀的程序员,十分优秀!