大家好,春季安全(Java的配置)問題
我有一個任務,我要創建3頁: /登錄 - 在那裏我們有電子郵件地址和密碼輸入, /結果 - 我們必須告訴用戶他是否已驗證,如果成功,我們可以顯示第3頁 - /dataEntry我們可以在DataBase中保存或更新用戶的信息。
典型項目的差異是用戶的電子郵件和密碼在users.xml中沒有數據庫(DB)
我已經薩克斯和DOM解析它。
分析器返回HashMap的其中 '關鍵' 是 '電子郵件 '和' 值' 是 '密碼'。
比我默認域:
1)Login.class - 是主類,以權威性,並只與users.xml中工作。它有下一個領域:電子郵件,密碼。
2)User.class - 使用DB(保存,更新,加載用戶信息)。它具有下列字段:身份證,電子郵件,名字,第二名,性別。
接下來我做了dao和服務層的這個域。 在我問的底部,我會給一個bitbucket的鏈接,但請閱讀我的問題。
我用Java的配置項目,所以我也Hibernate配置(它的工作原理是正確的),Web配置(好像它正常工作太)和安全配置(此時我要開始哭) 。
我的安全配置:
SecurityWebApplicationInitializer
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
public SecurityWebApplicationInitializer() {
}
SecurityConfiguration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
/**
* Holds userDetailsService
*/
@Autowired
@Qualifier("customUserDetailsService")
UserDetailsService userDetailsService;
/**
* Gets BCryptPasswordEncoder object.
*
* @return BCryptPasswordEncoder object.
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
/**
* Gets DaoAuthenticationProvider with its parameters
*
* @return authenticationProvider
*/
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userDetailsService);
authenticationProvider.setPasswordEncoder(passwordEncoder());
return authenticationProvider;
}
/**
* Sets GlobalSecurity parameters.
*
* @param auth - AuthenticationManagerBuilder object.
* @throws Exception
*/
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
/**
* Sets Encoding parameters to work with russian locale, filters to get access to any page.
* /index is login and logout page by default - everybody can open this page.
* /result is page with results of login - everybody can open this page.
* /dataEntry is page to save/update/load user's info - only registered user can open this page.
*
* @param http - {@link HttpSecurity} object
* @throws Exception
*/
@Override
public void configure(HttpSecurity http) throws Exception {
//To work with UTF-8 and RU locale
CharacterEncodingFilter f = new CharacterEncodingFilter();
f.setEncoding("UTF-8");
f.setForceEncoding(true);
http
.addFilterBefore(f, CsrfFilter.class)
.formLogin().loginPage("/index").defaultSuccessUrl("/result")
.usernameParameter("email").passwordParameter("password")
.and().logout().logoutSuccessUrl("/index").invalidateHttpSession(true)
.and().httpBasic().realmName("ArtezioWebApp")
.and().authorizeRequests()
.antMatchers("/", "/index", "/result/**").permitAll()
.antMatchers("/result/**").hasAnyAuthority("ROLE_USER","ROLE_ANONYMOUS")
.antMatchers("/dataEntry/**").hasAuthority("ROLE_USER")
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/result?error");
}
CustomUserDetailsService
public class CustomUserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
/**
* Holds logger.
*/
private static final Logger logger = LoggerFactory.getLogger(CustomUserDetailsService.class);
/**
* Holds {@link LoginService} object
*/
@Autowired
@Qualifier("loginService")
private LoginService loginService;
@Autowired
@Qualifier("login")
Login login;
/**
* Gets UserDetailsService object with parameters - email, password, authorities.
*
* @param email - by default has alias 'userName'
* @return UserDetailsService object with email,password and authorities.
* @throws UsernameNotFoundException if user was not found in *.xml file.
*/
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
//All users emails and passwords
HashMap<String, String> h = loginService.getUsers();
logger.info("Searching user with email '{}'...", email);
if (loginService.isValidEmail(email)) {
logger.info("User with email '{}' was found.", email);
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
//Saves data in Login object
login.setPassword(h.get(email));
login.setEmail(email);
return new org.springframework.security.core.userdetails.User(login.getEmail(),
login.getPassword(), true, true, true, true, authorities);
}
throw new UsernameNotFoundException("User with email '" + email + "' not found.");
}
當我調試項目時,我發現@Overloaded方法loadByUsername(字符串電子郵件)永遠不會被調用。
即使我輸入了正確的電子郵件和密碼,SecurityContext也會返回anonymusUser。 所以我不能訪問/ dataEntry頁面。
LINK TO到位桶:Bitbucket
有人請幫助我。 非常感謝。