spring security 用了自定义的UserDetailService后取不到已登录的用户信息了,每次调用SecurityContextHolder.getContext().getAuthentication()返回的信息都是null ..这可怎么办啊?
但是认证确实是通过了
但是认证确实是通过了
解决方案 »
- jsp插入数据问题
- JAVA WEB乱码怎么解决
- hibernate初始化!
- jsp
- Tomcat4与Tomcat5有什么区别?为什么我安装的tomcat4启动后没有反映?
- MySQL连localhost都连不上...........
- 使用Datetimepicker控件为什么出现这种情况?
- win2000+resin+oracle9i 如何使用连接池?急!!!
- jb7中使用的是tomcat4我想改为4.18怎么做呀!还有tomcat在jb中问题
- 菜鸟刚学jsp,还不知道怎么和sql server2000的数据库相连,我已经把odbc配好了,谁能给我一段和数据库相连的并显示所有数据的代码!菜鸟
- 关于struts2标签和html标签使用问题
- struts2_jQuery validate的提交问题---在线等
public class BigDTSessionManagmentFilter extends GenericFilterBean {
/**
* 过滤器方法,主要在Security的调用栈中将登录用户信息保存至HttpSession中。
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpSession session = req.getSession(false);
SecurityContext context = SecurityContextHolder.getContext();
String userName = "";
ThreadLocalUtil.putRequest(req); Authentication auth = null;
if (context != null) {
auth = context.getAuthentication();
if (auth == null || !(auth.getPrincipal() instanceof User)) {
userName = ((String) ((HttpServletRequest) request).getAttribute("authUser")).trim();
// 通过Servlet上下文获得Spring应用上下文,并获得AuthenticationManager对象
WebApplicationContext webApplicationContext = WebApplicationContextUtils
.getWebApplicationContext(this.getServletContext());
AuthenticationManager authenticationManager = (AuthenticationManager) webApplicationContext
.getBean("authenticationManager");
// 伪造通过认证的安全对象并将安全对象存入安全上下文
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(userName, "");
WebAuthenticationDetails webDetails = new WebAuthenticationDetails(req);
authToken.setDetails(webDetails);
auth = authenticationManager.authenticate(authToken);
context.setAuthentication(auth);
} else {
// auth不为null,说明通过认证
userName = ((User) auth.getPrincipal()).getUsername();
if (userName == null || userName.trim().length() == 0) { }
} if (session != null && session.getAttribute("securityUser") == null) {
session.setAttribute("securityUser", userName);
}
} try {
chain.doFilter(request, response);
} catch (AccessDeniedException e) {
e.printStackTrace();
}
}}我不是很清楚,这是手边的一段代码,伪造证书那段应该会有用吧