【问题标题】:Spring can't unmarshall bean when using Spring security使用 Spring 安全性时 Spring 无法解组 bean
【发布时间】:2012-12-03 17:27:58
【问题描述】:

我有一个从 org.springframework.security.core.userdetails.User 扩展而来的 bean,并将这个 bean 与 openID 一起使用。 bean 看起来像:

package com.employee;

import java.util.Collection;

import javax.xml.bind.annotation.XmlRootElement;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

@XmlRootElement(name="Employee")
public class Employee extends User{

    private int empId;
    private String deptName;

    public Employee() {
        super("", "unused", null);
    }
     public Employee(String username, Collection<GrantedAuthority> authorities) {
           super(username, "unused", authorities);
       }

    public int getEmpId() {
        return empId;
    }
    public void setEmpId(int empId) {
        this.empId = empId;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
}

控制器接受 Employee 的实例:

@RequestMapping(value = "/addEmp.do", 方法 = RequestMethod.POST) 公共@ResponseBody String addEmployee(@RequestBody Employee emp) {...}

我发送了一个带有以下正文的 POST 请求:

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
    <empId>10</empId>
    <deptName>abc</deptName>
</Employee>

但在调用 addEmp 操作时出现以下 JAXB 异常:

2012-12-16 15:25:55,364{HH:mm:ss} DEBUG [http-bio-8080-exec-1] (AbstractHandlerExceptionResolver.java:132) -
Resolving exception from handler [public java.lang.String com.main.EmpController.addEmployee(com .employee.Employee)]: 
org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to 
[class com.employee.Employee]: Unable to create an instance of com.employee.Employee; nested exception is 
javax.xml.bind.UnmarshalException: Unable to create an instance of com.employee.Employee

【问题讨论】:

  • 您确定 JAXB 的日志中没有更多内容吗?您是否尝试过使用此输入手动调用 JAXB(在某些游乐场项目中)?

标签: spring jaxb spring-security


【解决方案1】:

错误信息:

无法创建 com.employee 的实例

可能暗示输入未提供有效的员工属性。在构造函数中

public Employee() {
    super("", "unused", null);
}

默认用户名设置为'',但spring security不接受空白用户名。

【讨论】:

  • 这看起来可能是导致此问题的原因,因为在我介绍 userdetails 内容之前它一直有效。将尝试为用户名传递一些虚拟值,看看它是否修复。我没有使用用户名,所以我将其留空。可能还需要为每个用户生成唯一的用户名。
  • 您可以使用 empId.t​​oString() 作为用户名,如果它是唯一的且不可为空。此外,您可能需要提供一些其他属性,如密码、启用(T/F)、密码过期、帐户过期、帐户锁定等,如果它们未设置为可空。
  • empId.t​​oString() 在构造函数中不起作用,因为 empId 是成员变量。您能否让我知道如何将密码、启用等字段设置为可为空?我什至试过“super(String.valueOf((new Random()).nextInt()), “unused”, null);”但它没有用。
  • 密码等属性,参考API文档:static.springsource.org/spring-security/site/docs/3.0.x/apidocs/…,构造函数如下:User(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection extends GrantedAuthority> 权限)。您的 super() 方法可能会遗漏一些属性。 Random() 适用于少量数据,但对于大量数据,UUID 或 ObjectId 效果更好。
猜你喜欢
  • 1970-01-01
  • 2015-12-07
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-24
  • 1970-01-01
  • 2016-04-21
相关资源
最近更新 更多