Answer the question
In order to leave comments, you need to log in
How to rewrite Spring config from xml to Java?
I started learning Spring, I am doing a project on Spring-Boot, everything was going well, until I started doing authorization and registration according to the video lesson .
I reached the moment when the author Evgeny Suleimanov began to implement xml configs
Tell me
how to correctly rewrite these configs to JavaConfig under Spring Boot?
Are there any services or utilities for converting xml to JavaConfig ?
--- UPD ---
At the moment, I rewrote only appconfig-security.xml
I did the following:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests()
.antMatchers("/","welcome").hasAnyRole("ROLE_USER", "ROLE_ADMIN")
.antMatchers("/admin/**").hasRole("ROLE_ADMIN").anyRequest()
.authenticated().and()
.formLogin()
.loginPage("/login").failureUrl("/login?error")
.defaultSuccessUrl("/welcome")
.usernameParameter("username")
.passwordParameter("password")
.and().logout()
.logoutSuccessUrl("login?logout");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(userDetailsService()).passwordEncoder(
passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
@Bean
public UserDetailsService userDetailsService() {
return new UserDetailsServiceImpl();
}
}
I really don't know if I did everything right
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question