- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 spring security3 和 spring mvc3 来构建一个 web 项目。有一个叫index.jsp的页面,会显示登录用户名和在线用户数
此屏幕的顶部。登录系统有两种方式:
@Autowired
@Qualifier("authenticationManager")
AuthenticationManager authenticationManager;
@Autowired
SecurityContextRepository repository;
@RequestMapping(value="/ajaxLogin")
@ResponseBody
public String performLogin(
@RequestParam("j_username") String username,
@RequestParam("j_password") String password,
HttpServletRequest request, HttpServletResponse response) {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
try {
Authentication auth = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(auth);
repository.saveContext(SecurityContextHolder.getContext(), request, response);
logger.info("Authentication successfully! ");
return "{\"status\": true}";
} catch (BadCredentialsException ex) {
return "{\"status\": false, \"error\": \"Bad Credentials\"}";
}
}
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/index" access="permitAll" />
<form-login login-page="/login" default-target-url="/index"
authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
<session-management invalid-session-url="/index">
<concurrency-control max-sessions="1"
error-if-maximum-exceeded="false" />
</session-management>
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="
select login_id,login_pwd, is_enabled
from t_user where login_id=?"
authorities-by-username-query="
select u.login_id, r.authority from t_user u, t_roles r
where u.u_id = r.u_id and u.login_id =? " />
</authentication-provider>
</authentication-manager>
public class BaseController {
protected Logger logger = Logger.getLogger(this.getClass());
@Autowired
SessionRegistry sessionRegistry;
@ModelAttribute("numUsers")
public int getNumberOfUsers() {
logger.info("in getNumberOfUsers() ...");
return sessionRegistry.getAllPrincipals().size();
}
}
<div>
<security:authorize ifAllGranted="ROLE_USER">
<p><a href="#TODO">Welcome <security:authentication property="principal.username" />!</a>
<a href="<c:url value="/j_spring_security_logout" />">Logout</a></p>
</security:authorize>
</div>
<div style="color:#3CC457">
${numUsers} user(s) are logged in!
</div>
public class SessionCounterListener implements HttpSessionListener {
private Logger logger = Logger.getLogger(this.getClass());
private static int totalActiveSessions;
public static int getTotalActiveSession(){
return totalActiveSessions;
}
@Override
public void sessionCreated(HttpSessionEvent event) {
totalActiveSessions++;
logger.info("sessionCreated - add one session into counter" + event.getSession().getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
totalActiveSessions--;
logger.info("sessionDestroyed - deduct one session from counter" + event.getSession().getId());
}
sessionDestroyed - deduct one session 1spueddcmdao019udc43k3uumw
sessionCreated - add one session 14nro6bzyjy0x1jtvnqjx31v1
sessionDestroyed - deduct one session 14nro6bzyjy0x1jtvnqjx31v1
sessionCreated - add one session e6jqz5qy6412118iph66xvaa1
最佳答案
当您手动添加 Principal
至 SecurityContext
,它不会将用户添加到 SessionRegistry
.您需要将用户 session 添加到 SessionRegistry
手动。
SecurityContextHolder.getContext().setAuthentication(auth);
sessionRegistry.registerNewSession(request.getSession().getId(), auth.getPrincipal());
关于ajax - 手动身份验证时,使用 sessionRegistry 登录用户不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18271727/
我做了以下事情: 登录到我的 spring 应用程序(例如,作为用户“admin”) 停止tomcat 现在我看到 session 被序列化到 sessions.ser 文件中 我重启tomcat s
我有一个完全由 Weblogic 容器保护的 Web 应用程序。现在我必须列出当前登录的用户。我必须为此使用 Spring Security 2.0.4 在 web.xml 中我定义了必要的监听器和过
我使用 spring security3 和 spring mvc3 来构建一个 web 项目。有一个叫index.jsp的页面,会显示登录用户名和在线用户数 此屏幕的顶部。登录系统有两种方式: 从登
使用 @Autovired SessionRegistry sessionRegistry 在我的代码中,我添加了以下配置: http.sessionManagement() .max
我在 Spring Boot 应用程序中使用 Spring Security 来提供用户功能。我花了一些时间寻找问题的答案,但只为使用基于 xml 的配置的人找到了解决方案。 我的设置与此非常相似:h
我似乎找不到如何在 Struts 操作中获取对 Spring Security (V3) SessionRegistry 的引用。 我已经在我的 web.xml 文件中配置了监听器: org
有人可以提供关于如何仅使用 java 配置在 Spring Security 中获取非空 SessionRegistry 对象(没有任何 XML)的真实工作代码片段。 我正在使用 Spring Sec
在我写的http标签内: 我有以下 sas bean 在代码中我从注册表获取信息: @Autowired private SessionRegistr
我们的应用过去只有一种登录方式:用户名和密码。一旦新用户登录到应用程序,他们的 session 将出现在 Spring Security 的 SessionRegistry 中。 现在我正在 Spri
我正在使用 Hazelcast 3.4 和 Spring security 3.2.5。当我部署应用程序并尝试登录时,登录成功但抛出异常,导致错误页面。无论错误页面如何,我都已登录并且我的 sessi
问题场景: 我正在尝试使用带有 Java 配置的 Spring Security v3.2.3 来配置 session 管理,以便将 maximumSessions 设置为 1 并将 maxSessi
我正在使用 spring security 进行自定义身份验证。我没有使用任何 AuthenticationProvider,但使用 UsernamePasswordAuthenticationTok
也许应该配置 session 过期时间,或者我的代码中有错误? 我的 Controller 代码片段。即使已注销,它也会显示用户名。 @GetMapping("/") public String
我是一名优秀的程序员,十分优秀!