- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Spring MVC 4 项目中有一个 CustomLoginSucessHandler
来管理用户登录时的操作。
这工作正常。在同一个类中,我有方法 determineTargetUrl
来根据他的 ROLE
重定向用户。
代码如下:
@Override
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response){
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
final String userName = authentication.getName();
log.debug("TARGET URL METHOD!");
List<Authority> authorityList = authorityService.getAllAuthoritiesByUserName(userName);
for(Authority authority: authorityList){
switch (authority.getAuthority()){
case "ROLE_ADMIN":
return "processFile";
case "ROLE_USER":
return "userPortal";
case "ROLE_DEMO1":
return "processFile";
case "ROLE_DEMO2":
return "processFile";
}
}
return "403";
}
看到我有一个 log.debug("TARGET URL METHOD")
这个日志永远不会被调用,当然页面也不会被重定向,它会转到默认的登录页面,即 processFile.html
。
我很困惑为什么在我的 onAuthenticationSuccess
完美运行时没有调用第二种方法。他们在同一个类(class)。
这是我如何创建我的 CustomLoginSucessHandler
实例的代码:
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Autowired
private CustomLoginSucessHandler customLoginSucessHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().formLogin().loginPage("/login.html")
.loginProcessingUrl("/login").permitAll().and().logout().logoutSuccessUrl("/")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll().and().exceptionHandling()
.accessDeniedPage("/403.html");
http.csrf().requireCsrfProtectionMatcher(new CsrfRequestMatcher());
http.formLogin().successHandler(customLoginSucessHandler);
}
谢谢。
最佳答案
您正试图覆盖错误的函数,这是问题的根本原因。在您提供的摘录中,您有一个函数似乎覆盖另一个:
@Override
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response){
但实际上它并没有覆盖任何东西。 如果您检查 AuthenticationSuccessHandler 的 javadoc ,您会看到它只提供一个功能:onAuthenticationSuccess
,您将其报告为“有效”。它有效,但它是一个覆盖函数,它确实作为标准登录过程的一部分被调用。如果您密切关注此示例:
CustomLoginSuccessHandler example (可能你已经关注了这个)
您会看到 determineTargetUrl
函数没有被覆盖,而是被实现显式调用:
protected void handle(HttpServletRequest request,
HttpServletResponse response, Authentication authentication) throws IOException {
String targetUrl = determineTargetUrl(authentication);
依次从以下位置调用哪个句柄方法:
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication) throws IOException {
handle(request, response, authentication);
clearAuthenticationAttributes(request);
}
关于java - 未调用 Spring Security 4 中的方法 determineTargetUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620026/
我的 Spring MVC 4 项目中有一个 CustomLoginSucessHandler 来管理用户登录时的操作。 这工作正常。在同一个类中,我有方法 determineTargetUrl 来根
我是一名优秀的程序员,十分优秀!