java - 身份验证后存储附加信息[Spring]

标签 java spring spring-mvc authentication spring-security

我正在开发具有以下要求的 Web 应用程序:

  1. 允许用户登录
  2. 在服务器端,用户通过第 3 方 REST 网络服务进行身份验证。
  3. 如果身份验证成功,REST 网络服务将返回一个唯一的 token 和 key 。
  4. 对 REST 网络服务的任何后续请求都必须包含在第 3 点(上文)中收到的 token 。

我正在为 Web 应用程序使用 spring-mvc 和 spring security。 所以,我得到了一个有效的解决方案,但是我是 spring 的新手,不确定解决方案是否正确。

有人可以建议如果:

  1. 解决方案是否得到正确实现?
  2. 该解决方案是否以任何方式影响性能?
  3. 该解决方案是否会产生任何安全漏洞?

谢谢 :)


解决方案:

  1. 我创建了一个 MyUser 对象,用于存储从 REST 服务收到的附加信息。

    public class MyUser implements Serializable {
    
        private static final long serialVersionUID = 5047510412099091708L;
        private String RestToken;
        private String RestKey;
    
        public String getRestToken() {
            return RestToken;
        }
        public void setRestToken(String restToken) {
            RestToken = restToken;
        }
        public String getRestKey() {
            return RestKey;
        }
        public void setRestKey(String restKey) {
            RestKey = restKey;
        }
    }
    
  2. 然后我创建了一个扩展 UsernamePasswordAuthenticationToken 的 MyAuthenticationToken 对象。该对象将在 CustomAuthenticationProvider 中使用(下面的第 3 点)。

    public class MyAuthenticationToken extends UsernamePasswordAuthenticationToken {
    
            private static final long serialVersionUID = 7425814465946838862L;
            private MyUser myUser;
    
            public MyAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities, MyUser myUser){
    
                super(principal, credentials, authorities);
                this.myUser = myUser;   
            }
    
            public MyUser getMyUser() {
                return myUser;
            }
    
            public void setMyUser(MyUser myUser) {
                this.myUser = myUser;
            }
        }
    
  3. 我创建了一个自定义身份验证提供程序,它将调用 REST 服务进行身份验证,然后将附加信息存储在 myUser 和 myAuthenticationToken 对象中。

    public class CustomAuthenticationProvider implements AuthenticationProvider {
    
            @Override
            public Authentication authenticate (Authentication authentication) {
    
                MyUser myUser = new MyUser();
                MyAuthenticationToken authenticationToken = null;
                String name = authentication.getName();
                String password = authentication.getCredentials().toString();
    
                //Just an example. This section will connect to a web service in order to authenticate the client
                if (name.equals("justh") && password.equals("123456")) {
    
                    //Set the Token and Key Received from the REST WebService
                    myUser.setRestKey("RestKey");
                    myUser.setRestToken("RestToken");
    
                    List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
                    grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
    
                    authenticationToken = new MyAuthenticationToken(name, password, grantedAuths, myUser);
    
                    return authenticationToken;
                } else {
                    return null;
                }
            }
    
  4. 最后,我可以访问存储在我的 Controller 中的数据

    public ModelAndView adminPage(Authentication authentication) {
    
            MyUser user = null;
            //Get the additional data stored
            if(authentication instanceof MyAuthenticationToken){
                user = ((MyAuthenticationToken)authentication).getMyUser();
            }
    
            ModelAndView model = new ModelAndView();
            model.addObject("title", "Spring Security Hello World");
            model.addObject("message", "This is protected page - Admin Page!" + authentication.getName() + user.getRestKey() + user.getRestToken());
            model.setViewName("admin");
            return model;
        }
    

最佳答案

您的方法是正确的。只要您的要求超出简单的用户名密码身份验证流程,您就应该实现自定义 AuthenticationManagerAuthentication

但不要忘记遵守 AuthenticationManager的接口(interface)契约。

我在 webmailer 中做了一些非常相似的事情用于使用 javax.mail 对 smtp/imap 服务器进行身份验证,它可以完美地工作。

关于java - 身份验证后存储附加信息[Spring],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28010077/

相关文章:

java - 如何修复JDK11 Tomcat9 Spring Application上的JAVAX运行时错误

java - 池连接 tomcat 的 spring boot 中的 minIdle 属性在哪里?

Tomcat 服务器上的 java.lang.IllegalArgumentException 错误 -SpringMVC

java - 如何在 spring-servlet.xml 中配置 jackson object-mapper 以进行 @RequestBody 转换?

java - 服务类 "WBSer_RwCnt.Rw_Count"不符合 JAX-RPC 1.1 规范的一项或多项要求

java - 每秒更新一次 MySQL 数据库

java - 用于登录的简单易懂的 spring 应用程序

java - HashMap 中的 Jackson 日期格式

java - 重新启动 TailerListener 时如何避免旧日志消息

java - 产生 5 万个线程的可扩展性指南