- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
(编辑澄清)我有一个 POJO (SessionStorage) 来存储 session 特定数据,我想在成功验证后填充这些数据。由于我将 Scope 设置为“session”,我期望 MainController 和 AuthenticationSuccesshandler 使用相同的对象实例。
当我运行 WebApp 时,主 Controller 启动一个实例(如预期的那样),但是当我登录时,AuthenticationSuccesshandler 似乎没有 Autowiring SessionStorage 对象,因为它抛出 NullPointerException。
如何让它做我想做的事?以下是我的代码摘录:
@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SessionStorage implements Serializable{
long id;
public int getId() {
return id;
}
public SessionStorage() {
System.out.println("New Session Storage");
id = System.currentTimeMillis();
}
}
主 Controller 如下所示:
@Controller
@Scope("request")
@RequestMapping("/")
public class MainController {
@Autowired
private SessionStorage sessionStorage;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
System.out.println(sessionStorage.getId()); //Works fine
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "Invalid username and password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
model.setViewName("login");
return model;
}
}
AuthentificationSuccesshandler(抛出错误的地方):
public class AuthentificationSuccessHandler implements AuthenticationSuccessHandler {
@Autowired
private SessionStorage sessionStorage;
@Override
public void onAuthenticationSuccess(HttpServletRequest hsr, HttpServletResponse hsr1, Authentication a) throws IOException, ServletException {
System.out.println("Authentication successful: " + a.getName());
System.out.println(sessionStorage.getId()); //NullPointerException
}
}
spring-security.xml相关部分:
<beans:bean id="authentificationFailureHandler" class="service.AuthentificationFailureHandler" />
<beans:bean id="authentificationSuccessHandler" class="service.AuthentificationSuccessHandler" />
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/secure/**" access="hasRole('USER')" />
<form-login
login-page="/login"
default-target-url="/index"
authentication-failure-handler-ref="authentificationFailureHandler"
authentication-failure-url="/login?error"
authentication-success-handler-ref="authentificationSuccessHandler"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf/>
</http>
web-xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
最佳答案
这个问题很旧,但在 Google 中显示为指向我的问题的第一个链接。
我发现最有效的修复方法是在自定义 AuthenticationSuccessHandler 上设置范围。
@Component
@Scope(value="session", proxyMode = ScopedProxyMode.TARGET_CLASS)
可在此处找到更多详细信息: https://tuhrig.de/making-a-spring-bean-session-scoped/
关于java - 在 AuthenticationSuccessHandler 中设置 session 范围的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30697202/
我刚刚在这个论坛的帮助下添加了一个 AuthenticationSuccessHandler,它在用户通过 fosuserbundle 或 fosfacebookbundle 登录时在我的网站上实现重
这是我的 AuthSuccessHandlerClass public class AuthSuccessHandler implements AuthenticationSuccessHandler
我正在使用 Spring MVC 网站并通过 LDAP 添加 Active Directory 身份验证。该公司不想使用 AD 权限来映射网站的权限,我们有一个列出每个用户权限的数据库,所以我试图连接
我实现了一个成功处理程序,如果用户是管理员用户,该处理程序会将用户重定向到特定页面。 public class MaunaKeaAuthenticationSuccessHandler impleme
我有一个使用 Spring 3.1 的 java webapp。我的 Spring 安全上下文定义了多个身份验证过滤器,每个过滤器对应一个不同的身份验证路径(例如,用户名/密码与单点登录)。每个身份验
我想为用户提供一种使用 token 登录的替代方法。我已经在 PreAuthenticationFilter 类中处理了对用户的身份验证,但是我需要触发我的 AuthenticationSuccess
你好 Stackoverflower, 我遇到了 Spring Security AuthenticationSuccessHandler 的问题。我实现了自定义 AuthenticationSucc
我在 Spring Boot 应用程序中使用了 Spring Security,有两种类型的用户:一种是 ADMIN,另一种只是普通用户。我从 DataSource 获取数据,然后执行 SQL 查询。
在其他一些帖子之后,我尝试覆盖 spring-security 处理程序的身份验证成功方法,但它从未被调用。我的代码如下所示: src/groovy/mypackage/MyAuthenticatio
在我的 Spring Security 应用程序中,我尝试在成功登录后返回 cookie 'remember_token'。我的 AuthenticanSuccessHandler 类自动连接 Rem
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
我有一个包含所有帐户信息的 Customer 类。(它不扩展 Spring 的 userdetails.User 类)我正在尝试在成功登录后做一些事情(例如设置新的上次登录时间)。为此,我设置了自定义
我想为我的登录过滤器实现自定义 AuthenticationSuccessHandler,即 org.springframework.security.web.authentication.remem
我正在使用 Spring boot + data rest 设置纯 json rest 服务,现在无法让我的自定义身份验证成功处理程序(以及身份验证失败处理程序)来处理登录响应。登录本身可以正常工作,
(编辑澄清)我有一个 POJO (SessionStorage) 来存储 session 特定数据,我想在成功验证后填充这些数据。由于我将 Scope 设置为“session”,我期望 MainCon
我是 Spring Security 3 的新手。我正在使用角色让用户登录。 我想根据用户的角色将用户重定向到不同的页面,我的理解是我必须为此实现 AuthenticationSuccessHandl
我有一个自定义的 AuthenticationSuccessHandler。 我想做的是在 onAuthenticationSuccess 方法中设置一些 session 数据。 为了存储 sessi
我有一个 Spring-MVC 应用程序(即我正在使用 Spring 的调度程序 servlet)。我还使用 Spring Security 来验证用户身份。由于我使用 Spring 的调度程序 se
我有三个角色,我想在登录后根据用户的角色将用户重定向到不同的页面。我知道这可以通过 AuthenticationSuccessHandler 来完成,但我在基于 Java 的配置中声明它时遇到了麻烦。
@Component("MyAuthFilter") public class MyAuthFilter extends UsernamePasswordAuthenticationF
我是一名优秀的程序员,十分优秀!