关于Spring Security 手动设置Authentication的问题

漫雪信步 2015-08-06 03:10:22
applicationContext-security.xml:
<b:bean id="customFilter"
class="com.mybatis.security.FilerSecurityInterceptor">
<b:property name="authenticationManager"
ref="authenticationManager"/> <!-- 认证管理 -->
<b:property name="accessDecisionManager"
ref="customAccessDecisionManager"/><!-- 访问决策管理 -->
<b:property name="securityMetadataSource"
ref="customSecurityMetadataSource"/> <!--安全性元数据源 -->
</b:bean>

UserAccessDecisionManager( 访问决策管理 )里的方法:
public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes)
throws AccessDeniedException, InsufficientAuthenticationException {。。。}
authentication这个当前用户信息是空的它取的是默认的这值;我现在想手动添加当前用户信息;

我的这个场景是移动端上传数据(带用户信息到服务端代理GenericServlet,在GenericServlet中启动Spring容器,然后调用相应实际的servlet;

请高手帮忙看一下,谢了;

...全文
9511 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cheered_rock 2016-08-30
  • 打赏
  • 举报
回复
引用 1 楼 Giberson1 的回复:
一般的身份认证 从Spring Security核心部分,对Spring Security有一个笼统概念了,那该怎么理解上面说的呢? 通常情况下,我们的系统都是这样的: 1、用户输入用户名、密码登录 2、系统对用户名、密码进行验证 3、获取用户上下文信息(角色列表等等) 4、获取相关操作权限 对于上面说的前三条,用Spring Security来处理,就是: 1、用户名、密码组合生成一个Authentication对象(也就是UsernamePasswordAuthenticationToken对象)。 2、生成的这个token对象会传递给一个AuthenticationManager对象用于验证。 3、当成功认证后,AuthenticationManager返回一个Authentication对象。 4、接下来,就可以调用 SecurityContextHodler.getContext().setAuthentication(…)。 为了更好的理解,下面就写一个例子:
package com.springsecurity.java.test;
 
 
 
import java.io.BufferedReader;
 
import java.io.InputStreamReader;
 
import java.util.ArrayList;
 
import java.util.List;
 
 
 
import org.springframework.security.authentication.AuthenticationManager;
 
import org.springframework.security.authentication.BadCredentialsException;
 
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 
import org.springframework.security.core.Authentication;
 
import org.springframework.security.core.AuthenticationException;
 
import org.springframework.security.core.GrantedAuthority;
 
import org.springframework.security.core.authority.SimpleGrantedAuthority;
 
import org.springframework.security.core.context.SecurityContextHolder;
 
 
 
public class AuthenticationExample {
 
   private static SimpleAuthenticationManager samgr = new SimpleAuthenticationManager();
 
 
 
   public static void main(String[] args) {
 
      try {
 
// 用户输入用户名、密码:
 
        BufferedReader in = new BufferedReader(new InputStreamReader(
 
              System.in));
 
        System.out.println("Please enter your username:");
 
        String name = in.readLine();
 
        System.out.println("Please enter your password:");
 
        String password = in.readLine();
 
// 接下来是系统进行身份认证的过程:
 
//1、将用户名、密码封装成一个token
 
        Authentication token = new UsernamePasswordAuthenticationToken(
 
              name, password);
 
//2、将token传给AuthenticationManager进行身份认证
 
//3、认证完毕,返回一个认证后的身份:
 
        Authentication result = samgr.authenticate(token);
 
// 认证后,存储到SecurityContext里 :    SecurityContextHolder.getContext().setAuthentication(result);
 
      } catch (Exception ex) {
 
        System.out.println("认证失败");
 
      }
 
 
 
// 从SecurityContext读取认证的身份:
 
System.out.println(SecurityContextHolder.getContext()
 
           .getAuthentication());
 
   }
 
}
 
 
 
class SimpleAuthenticationManager implements AuthenticationManager {
 
   static final List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
 
   static {
 
      AUTHORITIES.add(new SimpleGrantedAuthority("ROLE_USER"));
 
   }
 
 
 
   public Authentication authenticate(Authentication auth)
 
        throws AuthenticationException {
 
      if (auth.getName().equals(auth.getCredentials())) {
 
        return new UsernamePasswordAuthenticationToken(auth.getName(),
 
              auth.getCredentials(),AUTHORITIES);
 
      }
 
      throw new BadCredentialsException("Bad Credentials");
 
   }
 
 
 
}
 
非常管用,3Q
漫雪信步 2015-08-07
  • 打赏
  • 举报
回复
你好 Authentication 对象现在能加进去了;但是authenticationManager节点的代码没有执行;(容器只调用了accessDecisionManager,securityMetadataSource这两个节点); 虽然SimpleAuthenticationManager已经做了authenticationManager节点做的事,但是只用accessDecisionManager,securityMetadataSource这两个节点,用户信息验证的部分是手动调用这样是不是有点怪;
Giberson1 2015-08-06
  • 打赏
  • 举报
回复
一般的身份认证 从Spring Security核心部分,对Spring Security有一个笼统概念了,那该怎么理解上面说的呢? 通常情况下,我们的系统都是这样的: 1、用户输入用户名、密码登录 2、系统对用户名、密码进行验证 3、获取用户上下文信息(角色列表等等) 4、获取相关操作权限 对于上面说的前三条,用Spring Security来处理,就是: 1、用户名、密码组合生成一个Authentication对象(也就是UsernamePasswordAuthenticationToken对象)。 2、生成的这个token对象会传递给一个AuthenticationManager对象用于验证。 3、当成功认证后,AuthenticationManager返回一个Authentication对象。 4、接下来,就可以调用 SecurityContextHodler.getContext().setAuthentication(…)。 为了更好的理解,下面就写一个例子:
package com.springsecurity.java.test;
 
 
 
import java.io.BufferedReader;
 
import java.io.InputStreamReader;
 
import java.util.ArrayList;
 
import java.util.List;
 
 
 
import org.springframework.security.authentication.AuthenticationManager;
 
import org.springframework.security.authentication.BadCredentialsException;
 
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 
import org.springframework.security.core.Authentication;
 
import org.springframework.security.core.AuthenticationException;
 
import org.springframework.security.core.GrantedAuthority;
 
import org.springframework.security.core.authority.SimpleGrantedAuthority;
 
import org.springframework.security.core.context.SecurityContextHolder;
 
 
 
public class AuthenticationExample {
 
   private static SimpleAuthenticationManager samgr = new SimpleAuthenticationManager();
 
 
 
   public static void main(String[] args) {
 
      try {
 
// 用户输入用户名、密码:
 
        BufferedReader in = new BufferedReader(new InputStreamReader(
 
              System.in));
 
        System.out.println("Please enter your username:");
 
        String name = in.readLine();
 
        System.out.println("Please enter your password:");
 
        String password = in.readLine();
 
// 接下来是系统进行身份认证的过程:
 
//1、将用户名、密码封装成一个token
 
        Authentication token = new UsernamePasswordAuthenticationToken(
 
              name, password);
 
//2、将token传给AuthenticationManager进行身份认证
 
//3、认证完毕,返回一个认证后的身份:
 
        Authentication result = samgr.authenticate(token);
 
// 认证后,存储到SecurityContext里 :    SecurityContextHolder.getContext().setAuthentication(result);
 
      } catch (Exception ex) {
 
        System.out.println("认证失败");
 
      }
 
 
 
// 从SecurityContext读取认证的身份:
 
System.out.println(SecurityContextHolder.getContext()
 
           .getAuthentication());
 
   }
 
}
 
 
 
class SimpleAuthenticationManager implements AuthenticationManager {
 
   static final List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
 
   static {
 
      AUTHORITIES.add(new SimpleGrantedAuthority("ROLE_USER"));
 
   }
 
 
 
   public Authentication authenticate(Authentication auth)
 
        throws AuthenticationException {
 
      if (auth.getName().equals(auth.getCredentials())) {
 
        return new UsernamePasswordAuthenticationToken(auth.getName(),
 
              auth.getCredentials(),AUTHORITIES);
 
      }
 
      throw new BadCredentialsException("Bad Credentials");
 
   }
 
 
 
}
 

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧