image.pngimage.png

    • 错误解释: 由于shiro中提供的simpleByteSource实现没有实现序列化,所有在认证时出现错误信息
    • 解决方案: 需要自动salt实现序列化


    • 实现 实体类 序列化
    • 自定义salt实现 实现序列化接口 ```java package com.cedric.springboot_jsp_shiro.shiro.salt;

    import org.apache.shiro.codec.Base64; import org.apache.shiro.codec.CodecSupport; import org.apache.shiro.codec.Hex; import org.apache.shiro.util.ByteSource;

    import java.io.File; import java.io.InputStream; import java.io.Serializable; import java.util.Arrays;

    //自定义salt的实现 实现序列化接口 public class MyByteSource implements ByteSource,Serializable {

    1. private byte[] bytes;
    2. private String cachedHex;
    3. private String cachedBase64;
    4. public MyByteSource(){
    5. }
    6. public MyByteSource(byte[] bytes) {
    7. this.bytes = bytes;
    8. }
    9. public MyByteSource(char[] chars) {
    10. this.bytes = CodecSupport.toBytes(chars);
    11. }
    12. public MyByteSource(String string) {
    13. this.bytes = CodecSupport.toBytes(string);
    14. }
    15. public MyByteSource(ByteSource source) {
    16. this.bytes = source.getBytes();
    17. }
    18. public MyByteSource(File file) {
    19. this.bytes = (new MyByteSource.BytesHelper()).getBytes(file);
    20. }
    21. public MyByteSource(InputStream stream) {
    22. this.bytes = (new MyByteSource.BytesHelper()).getBytes(stream);
    23. }
    24. public static boolean isCompatible(Object o) {
    25. return o instanceof byte[] || o instanceof char[] || o instanceof String || o instanceof ByteSource || o instanceof File || o instanceof InputStream;
    26. }
    27. public byte[] getBytes() {
    28. return this.bytes;
    29. }
    30. public boolean isEmpty() {
    31. return this.bytes == null || this.bytes.length == 0;
    32. }
    33. public String toHex() {
    34. if (this.cachedHex == null) {
    35. this.cachedHex = Hex.encodeToString(this.getBytes());
    36. }
    37. return this.cachedHex;
    38. }
    39. public String toBase64() {
    40. if (this.cachedBase64 == null) {
    41. this.cachedBase64 = Base64.encodeToString(this.getBytes());
    42. }
    43. return this.cachedBase64;
    44. }
    45. public String toString() {
    46. return this.toBase64();
    47. }
    48. public int hashCode() {
    49. return this.bytes != null && this.bytes.length != 0 ? Arrays.hashCode(this.bytes) : 0;
    50. }
    51. public boolean equals(Object o) {
    52. if (o == this) {
    53. return true;
    54. } else if (o instanceof ByteSource) {
    55. ByteSource bs = (ByteSource)o;
    56. return Arrays.equals(this.getBytes(), bs.getBytes());
    57. } else {
    58. return false;
    59. }
    60. }
    61. private static final class BytesHelper extends CodecSupport {
    62. private BytesHelper() {
    63. }
    64. public byte[] getBytes(File file) {
    65. return this.toBytes(file);
    66. }
    67. public byte[] getBytes(InputStream stream) {
    68. return this.toBytes(stream);
    69. }
    70. }

    }

    
    ```java
      @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    
            //根据身份信息
            String principal = (String) authenticationToken.getPrincipal();
            //在工厂中获取service对象
            UserService userService = (UserService) ApplicationContextUtils.getBean("userService");
            User user = userService.findByUserName(principal);
    
            if(!ObjectUtils.isEmpty(user)){
    
                return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),
                           new MyByteSource(user.getSalt()), this.getName());
            }
            return null;
        }
    

    image.png