shiro 使用緩存時出現:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource

在學習Shiro使用緩存時,出現:
java.io.NotSerializableException:org.apache.shiro.util.SimpleByteSource異常,開啓debug會提示:
ERROR [authentication.data] - Disk Write of test failed: 錯誤。

出現這種情況是因爲:SimpleByteSource沒有是實現Serializable接口

解決辦法:自定義一個類繼承SimpleByteSource實現Serializable接口

當然也可以實現ByteSource接口和Serializable接口,但是實現ByteSource接口需要實現其方法,不方便。

自定義一個MySimpleByteSource 類繼承繼承SimpleByteSource實現Serializable接口。

public class MySimpleByteSource extends SimpleByteSource implements Serializable {

    private static final long serialVersionUID = 1L;

    public MySimpleByteSource(byte[] bytes) {
        super(bytes);
    }
}

在自定義realm的認證方法中:

    //認證
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken token) throws AuthenticationException {
        //獲得主體對象身份
        String username = (String) token.getPrincipal();
        //根據用戶名查詢用戶信息
        User user = userDao.queryUserByUsername(username);
        if (user!=null) {
            return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), new MySimpleByteSource(user.getSalt().getBytes()), getName());
        }
        return null;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章