我正在尝试从ApplicationListener<AuthenticationSuccessEvent>成功登录时实现接口的类调用受保护的方法(Spring 3.2.2和Spring Security 3.2.0 M1).这是我之前的问题.
该应用程序在以下环境中运行.
我已将以下与Spring安全性相关的库添加到类路径中.
实现的类ApplicationListener<AuthenticationSuccessEvent>如下.
package loginsuccesshandler;
import admin.dao.service.StateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Service;
@Service
public final class AuthSuccessHandler implements ApplicationListener<AuthenticationSuccessEvent>
{
@Autowired
private StateService stateService;
@Override
public void onApplicationEvent(AuthenticationSuccessEvent event)
{
System.out.println(event.getAuthentication());
System.out.println("rowCount = "+stateService.rowCount());
}
}
Run Code Online (Sandbox Code Playgroud)
This prevents a user from being logged in even with correct credentials with the …
我是Spring Security的新手.如何添加将在用户成功登录时调用的事件侦听器?此外,我需要在此侦听器中获取某种唯一的会话ID,该ID应该可以进一步使用.我需要此ID与另一台服务器同步.
我是MySQL新手.我创建了一个包含列名的表,First Name但这是错误的.
现在我想将列名更改First Name为First_Name.可能吗?如果是,请解释我如何更改我的列名称.
我有一个Spring Boot应用程序,WebSecurityConfigurerAdapter配置如下 -
http.csrf().disable()
.exceptionHandling()
.authenticationEntryPoint(restAuthenticationEntryPoint)
.and()
.authorizeRequests()
.antMatchers("/user/*", "/habbit/*").authenticated()
.and()
.formLogin()
.loginProcessingUrl("/login")
.permitAll()
.usernameParameter("email")
.passwordParameter("pass")
.successHandler(authenticationSuccessHandler)
.failureHandler(new SimpleUrlAuthenticationFailureHandler())
.and()
.logout()
.logoutUrl("/logout")
.invalidateHttpSession(true);
Run Code Online (Sandbox Code Playgroud)
我可以添加类似我自己的控制器,在成功验证后,返回一个自定义对象,其中包含有关经过身份验证的用户的一些详细信息吗?
更新: 为了清晰起见,我使用角度应用程序作为客户端.目前,我需要从客户端向服务器发出2个请求:1.POST请求/登录URL进行身份验证.2. GET请求以检索经过身份验证的用户数据.
我的目标是让第一个请求返回给我用户信息,所以我不必提出2dn请求.目前,第一个请求仅对用户进行身份验证,在服务器上创建会话并发送回没有数据的"200 OK"状态响应.我希望它返回有关登录用户数据的成功响应.
回答:
正确的答案在评论中,所以我将在这里写:我需要从我的successHandler重定向到我的控制器,然后控制器返回当前登录的用户信息(在我的情况下控制器在url'/ user/me':
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws ServletException, IOException {
clearAuthenticationAttributes(request);
getRedirectStrategy().sendRedirect(request, response, "/user/me");
}
Run Code Online (Sandbox Code Playgroud)