我想配置一些休息服務的Spring啓動和彈簧安全。 我有表用戶,特權,priviledge_user 我有這個配置文件休息與春季開機+彈簧安全與自定義用戶表
WebSecurityConfig
@EnableWebSecurity
@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().fullyAuthenticated().and().
httpBasic().and().
csrf().disable();
}
}
和AuthenticationConfiguration
@Configuration
public class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
@Autowired
UserRepository userRepository;
Logger log = LoggerFactory.getLogger(this.getClass());
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService());
}
@Bean
UserDetailsService userDetailsService() {
return new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User account = userRepository.findByUsername(username);
log.info("The user ({}) logged has the password {}", username, account.getPassword());
org.springframework.security.core.userdetails.User userSession = new org.springframework.security.core.userdetails.User(account.getUsername(), account.getPassword(), true, true, true, true,
AuthorityUtils.createAuthorityList("USER"));
return userSession;
}
};
}
}
調試運行的代碼似乎驗證運作良好,因爲我已經從用戶數據庫(我正在使用JNDI datasoure,並在application.properties中配置) 但是當我試圖獲得任何東西時http://localhost:8080/並設置我的憑據我無法訪問,並始終獲得憑據提示 什麼我做得不好?
您是否擁有數據庫中的有效用戶?日誌記錄說什麼? – holmis83
是的我有一個有效的用戶在數據庫中,我沒有錯誤日誌只有 '2015-11-09 17:07:26,069信息[org.springframework.boot.actuate.audit.listener.AuditListener] (默認任務-2)AuditEvent [timestamp = Mon Nov 09 17:07:26 BOT 2015,principal = anonymousUser,type = AUTHORIZATION_FAILURE,data = {type = org.springframework.security.access.AccessDeniedException,message = Access is denied} ] ' – poloche