Java Spring安全性-身份验证不需要';我不能正常工作
我一直在努力在我的应用程序中进行身份验证,但不知为什么它不能正常工作。我确信我传递了正确的用户名和密码,但仍然无法通过应用程序正确验证 以下是安全配置:Java Spring安全性-身份验证不需要';我不能正常工作,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我一直在努力在我的应用程序中进行身份验证,但不知为什么它不能正常工作。我确信我传递了正确的用户名和密码,但仍然无法通过应用程序正确验证 以下是安全配置: @Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setPasswordEncoder(passwordEncoder());
authenticationProvider.setUserDetailsService(userDetailsService);
return authenticationProvider;
}
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
//auth.inMemoryAuthentication().withUser("admin1").password("admin").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.antMatchers("/account").hasRole("USER")
.and().formLogin().loginPage("/login").failureUrl("/login?error")
.usernameParameter("username").passwordParameter("password")
.and().logout().logoutSuccessUrl("/login?logout")
.and().exceptionHandling().accessDeniedPage("/403")
.and().csrf().disable();
}
}
用户服务:
@Service
@Transactional(rollbackFor = Exception.class)
public class UserService extends MainService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
private static final Logger LOGGER = Logger.getLogger(UserService.class);
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
LOGGER.debug("Attempt to log in user with given username: " + login);
User user = userRepository.findByAccountLogin(login);
if(user == null) {
throw new UsernameNotFoundException("User with login: " + login + " does not exist");
}
LOGGER.debug("Loaded account: " + user.getAccount() + " password matches: " + new BCryptPasswordEncoder().matches("admin", user.getAccount().getPassword()));
return new org.springframework.security.core.userdetails.User(user.getAccount().getLogin(), user.getAccount().getPassword(), user.getAccount().getActivated(), true, true, true, getGrantedAuthorities(user));
}
private List<GrantedAuthority> getGrantedAuthorities(final User user) {
List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
for(AccountRole role : user.getAccount().getRoles()) {
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_" + role.getId().getaRole().getType()));
}
LOGGER.debug("Granted authorities: " + grantedAuthorities);
return grantedAuthorities;
}
}
这是什么原因造成的?我是一个新手,所以如果我提出一些愚蠢的建议,请原谅我,但我自己刚刚建立了一个(工作)身份验证,所以我可能会有所帮助。我正在用XML配置所有内容,因此不确定我是否正确理解了这一点,不管怎样,您的SimpleGrantedAuthority被称为“角色\用户”,但您允许“/帐户”使用“用户”。或者它是这样工作的吗?你的密码实际上是加密存储在数据库中的吗?是的,它是加密的。我是一个新手,所以如果我提出一些愚蠢的建议,请原谅我,但我只是自己建立了一个(工作的)身份验证,所以我可能会有所帮助。我正在用XML配置所有内容,因此不确定我是否正确理解了这一点,不管怎样,您的SimpleGrantedAuthority被称为“角色\用户”,但您允许“/帐户”使用“用户”。或者它是这样工作的?你的密码实际上是加密存储在数据库中的吗?是的,它是加密的。
LOGGER.debug("Loaded account: " + user.getAccount() + " password matches: " + new BCryptPasswordEncoder().matches("admin", user.getAccount().getPassword()));