AuthenticationManager.authenticate方法未被调用
创始人
2024-09-22 14:30:50
0

检查是否正确配置了Spring Security,并且在代码中正确地调用了AuthenticationManager.authenticate方法。以下是一个示例代码:

@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .anyRequest().authenticated()
        .and()
        .formLogin().permitAll()
        .and()
        .logout().permitAll();
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

}

@Controller public class LoginController {

@Autowired
private AuthenticationManager authenticationManager;

@Autowired
private JwtTokenUtil jwtTokenUtil;

@PostMapping("/login")
public ResponseEntity login(@RequestBody LoginRequest loginRequest) {

    Authentication authentication = authenticationManager.authenticate(
            new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));

    SecurityContextHolder.getContext().setAuthentication(authentication);

    final UserDetails userDetails = userDetailsService
            .loadUserByUsername(loginRequest.getUsername());

    final String token = jwtTokenUtil.generateToken(userDetails);

    return ResponseEntity.ok(new LoginResponse(token));
}

}

在上面的示例代码中,我们将AuthenticationManager注入到LoginController中,并在login方法中正确地调用了AuthenticationManager.authenticate方法。同时,我们还需要在WebSecurityConfigurerAdapter中配置AuthenticationManager。

相关内容

热门资讯

据悉!三哥玩摆头注可以控制,越... 据悉!三哥玩摆头注可以控制,越乡游义乌辅助器微信免费,大纲教程(原来确实是有挂)-哔哩哔哩1、不需要...
为切实保障!闲玩暗宝辅助软件下... 为切实保障!闲玩暗宝辅助软件下载,火神大厅辅助器,窍要教程(原来是真的有挂的)-哔哩哔哩1、打开软件...
经核实!四川途游破解安装包,八... 经核实!四川途游破解安装包,八闽掌上十八卦脚本,诀窍教程(一贯确实有挂的)-哔哩哔哩1)八闽掌上十八...
据玩家消息!胡乐辅助脚本可卡片... 据玩家消息!胡乐辅助脚本可卡片吗,小闲辅助神器,攻略教程(一直是有挂)-哔哩哔哩运胡乐辅助脚本可卡片...
无独有偶!阿拉斗牌辅助视频,情... 无独有偶!阿拉斗牌辅助视频,情怀游戏字牌辅助,指引教程(一贯是有挂)-哔哩哔哩1、这是跨平台的情怀游...
经调查!水鱼辅助软件下载,丰城... 经调查!水鱼辅助软件下载,丰城呱呱辅助器,法门教程(一贯确实有挂)-哔哩哔哩1、水鱼辅助软件下载透视...
更值得关注的是!来几局免费辅助... 更值得关注的是!来几局免费辅助器,胡乐辅助脚本,步骤教程(一贯确实有挂)-哔哩哔哩1、来几局免费辅助...
更值得关注的是!欢乐达人正版脚... 更值得关注的是!欢乐达人正版脚本,新玄龙辅助,大纲教程(其实真的有挂)-哔哩哔哩进入游戏-大厅左侧-...
随着!广西微乐小程序辅助器,奇... 随着!广西微乐小程序辅助器,奇迹陕西辅助,学习教程(一贯确实有挂的)-哔哩哔哩1、下载好广西微乐小程...
日前!闲来辅助工具,新海贝辅助... 日前!闲来辅助工具,新海贝辅助器,方式教程(其实是有挂的)-哔哩哔哩1、新海贝辅助器透视辅助简单,新...