我使用Spring Session和JDBC和Mysql。Spring Security Login成功但主体名称为null。
有谁有想法,这是错误的吗?
文件:
application.yaml:
会议:…
我创建了一个单独的类来获取详细信息。你可以打电话 getCurrentUserDetails() 获取实例 USER
getCurrentUserDetails()
USER
@Component public class CurrentUser { @Autowired UserRepository userRepository; public User getCurrentUserDetails() { Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); String currentUsername; if (principal instanceof UserDetails) { currentUsername = ((UserDetails)principal).getUsername().toString(); User user = userRepository.findByEmail(currentUsername); return user; } else //return null or your code; } }
登录后,您的凭据将被删除。您可以阻止该密码,但不能找到密码 Principle 要么 SecurityContext 。在上面的配置文件中尝试以下代码:
Principle
SecurityContext
@Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.eraseCredentials(false); auth.userDetailsService(myUserDetailsService).passwordEncoder(new BCryptPasswordEncoder()); }
的 UPDATE 强>
添加以下bean并查看结果:
@Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); }
还请补充一下 的 的pom.xml 强> 内容给你提问。
如果您使用的是Thymeleaf,请将这些行添加到您的pom中:
<properties> <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version> <thymeleaf-extras-springsecurity4.version>3.0.4.RELEASE</thymeleaf-extras-springsecurity4.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties>
现在主要部分,您配置文件:
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableWebSecurity @EnableJdbcHttpSession @EnableJpaRepositories(basePackageClasses = UsersRepository.class) @Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired private CustomUserDetailsService userDetailsService; <strike>@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); }</strike> @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.eraseCredentials(false); auth.userDetailsService(myUserDetailsService).passwordEncoder(new BCryptPasswordEncoder()); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); http.authorizeRequests() .antMatchers("/*").authenticated().anyRequest().permitAll() .antMatchers("/sources/*").anonymous().anyRequest().permitAll() .antMatchers("/public/*").anonymous().anyRequest().permitAll() .and() .formLogin(). loginPage("/login"). loginProcessingUrl("/app-login"). usernameParameter("app_username"). passwordParameter("app_password"). permitAll() .and() .exceptionHandling(). accessDeniedPage("/error403"); } @Bean public BCryptPasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }
最后控制器:
@ResponseBody @GetMapping("/check") public String check(Principal principal) { System.out.println(principal.getName()); return "ok"; }