作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将 SSO 包括到我的 J2EE 项目中。我正在尝试找到不需要除我的应用程序服务器之外的其他服务器的解决方案。
这是环境:
项目实现了什么:
带有登录和测试方法的 Controller :
@Controller
public class ProjectController {
public static final String REMEMBER_ME_ACTIVE = "on";
@Autowired
SecurityContextRepository repository;
@Autowired
RememberMeServices rememberMeServices;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public Object login(HttpServletRequest request, HttpServletResponse response) {
System.out.println("[GET] login");
System.out.println(SecurityContextHolder.getContext().getAuthentication());
System.out.println(request.getSession().getId());
return new ModelAndView("login");
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public Object login(@RequestParam("j_username") String username,
@RequestParam("j_password") String password,
@RequestParam("_spring_security_remember_me") String rememberMe,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("[POST] login");
System.out.println(SecurityContextHolder.getContext().getAuthentication());
System.out.println(request.getSession().getId());
try {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(token);
repository.saveContext(context, request, response);
if (REMEMBER_ME_ACTIVE.equals(rememberMe)) {
rememberMeServices.loginSuccess(request, response, token);
}
return "OK";
}
catch (BadCredentialsException e) {
return "BadCredentialsException";
}
catch (Exception e) {
e.printStackTrace();
return "Exception : " + e.getMessage();
}
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public Object test(HttpServletRequest request, HttpServletResponse response) {
System.out.println("[GET] test");
System.out.println(SecurityContextHolder.getContext().getAuthentication());
System.out.println(request.getSession().getId());
return new ModelAndView("test");
}
}
spring-security.xml :
<?xml version="1.0" encoding="UTF-8"?>
<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.1.xsd">
<!-- security config -->
<http auto-config="false" use-expressions="true" >
<http-basic/>
<intercept-url pattern="/login" access="hasRole('ROLE_ANONYMOUS')"/>
<intercept-url pattern="/**" access="hasAnyRole('ROLE_USER')" />
<form-login login-page="/login"
login-processing-url="/spring/login"
authentication-failure-url="/login"
default-target-url="/"
always-use-default-target="true" />
<remember-me services-ref="tokenBasedRememberMeServices"
key="XXXXXX_1234567890"
use-secure-cookie="true" />
<logout logout-url="/spring/logout"
invalidate-session="false"
logout-success-url="/login" />
<session-management invalid-session-url="/login" session-fixation-protection="newSession">
<concurrency-control max-sessions="3"
error-if-maximum-exceeded="false"
expired-url="/login"
session-registry-ref="sessionRegistry" />
</session-management>
</http>
<beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />
<!-- authentication config -->
<authentication-manager>
<authentication-provider ref="userAuthenticationProvider"/>
</authentication-manager>
<beans:bean id="userAuthenticationProvider" class="be.xxx.spring.security.UserAuthenticationProvider" />
<beans:bean id="tokenBasedRememberMeServices" class="be.xxx.spring.security.TokenBasedRememberMeServices">
<beans:constructor-arg name="key" value="XXXXXX_1234567890"/>
<beans:constructor-arg name="userDetailsService" ref="userDetailsService"/>
</beans:bean>
<beans:bean id="userDetailsService" class="be.xxx.spring.security.UserDetailsService" />
</beans:beans>
到目前为止,我发现 Glassfish 支持 SSO,如 here 所写,但它似乎不能与 Spring Security 一起工作。在 project1 中登录时,我看不到 JSESSIONIDSSO。因此,当我启动 project2 时,它会向我发送登录表单...还尝试在 web.xml 中指定相同的领域名称,但没有任何更改。
在谷歌搜索之后,我发现了一些关于 PreAuthenticationFilter 的东西,但我真的不知道如何用它来实现 SSO 解决方案。
你能帮我找出解决办法吗?
谢谢,
烟熏
最佳答案
如果项目相同,您始终可以使用粘性 session 并在项目之间共享 session 。
关于java - Spring 安全单点登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19857502/
这个问题在这里已经有了答案: Git for beginners: The definitive practical guide (37 个答案) 关闭 9 年前。 在使用句点(或句点,单点)将文件
我是一名优秀的程序员,十分优秀!