【问题标题】:Jwt in Springboot v2.4.5Springboot v2.4.5 中的 Jwt
【发布时间】:2021-11-05 03:28:55
【问题描述】:

我有这个 RestController:

@RestController
@Slf4j
public class AuthenticationRestController {


    @Value("${jwt.header}")
    private String tokenHeader;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private UserSecurityService userSecurityService;

    @Autowired
    private EmailService emailService;


    @PostMapping(path = "/api/v1/auth", consumes = "application/json", produces = "application/json")
    public ResponseEntity<JwtAuthenticationResponse>
    createAuthenticationToken(  @RequestBody JwtAuthenticationRequest authenticationRequest,
            HttpServletRequest request) throws AuthenticationException {

        LOG.info("authenticating {} " , authenticationRequest.getUsername());

        authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());

...

    /**
     * Authenticates the user. If something is wrong, an {@link AuthenticationException} will be thrown
     */
    private void authenticate(String username, String password) {

        Objects.requireNonNull(username);
        Objects.requireNonNull(password);


        try {
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
        } catch (DisabledException e) {
            e.printStackTrace();
            throw new AuthenticationException("User is disabled!", e);
        } catch (BadCredentialsException e) {
            throw new AuthenticationException("Bad credentials!", e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

但我在登录时出现此错误:

org.springframework.security.authentication.DisabledException: User is disabled
    at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider$DefaultPreAuthenticationChecks.check(AbstractUserDetailsAuthenticationProvider.java:331)
    at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:146)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:182)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:201)
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:518)
    at com.kispackp.security.controllers.AuthenticationRestController.authenticate(AuthenticationRestController.java:138)

@Entity
@Table(name="t_user")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable, UserDetails {

    /** The Serial Version UID for Serializable classes. */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(unique = true)
    @JsonIgnore
    private String username;

    @JsonIgnore
    private String password;



    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

}

【问题讨论】:

    标签: spring-boot spring-mvc spring-security jwt


    【解决方案1】:

    当用户未启用时会发生此错误。接口UserDetails有一个方法叫isEnabled,在认证用户时会检查。

    AbstractUserDetailsAuthenticationProvider.java
    ...
    if (!user.isEnabled()) {
        AbstractUserDetailsAuthenticationProvider.this.logger
                .debug("Failed to authenticate since user account is disabled");
        throw new DisabledException(AbstractUserDetailsAuthenticationProvider.this.messages
                .getMessage("AbstractUserDetailsAuthenticationProvider.disabled", "User is disabled"));
    }
    ...
    

    您应该实现它并在用户启用的情况下返回true,如下所示:

    public class User implements Serializable, UserDetails {
    
        ... your current fields and methods
    
        @Override
        public boolean isEnabled() {
            return true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-05
      • 2019-01-10
      • 1970-01-01
      • 2018-01-30
      • 2022-10-05
      • 2018-11-20
      • 2022-01-11
      • 2022-07-22
      相关资源
      最近更新 更多