C
C
Chvalov2017-11-14 19:02:46
Java
Chvalov, 2017-11-14 19:02:46

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

1 answer(s)
P
PqDn, 2017-11-15
@PqDn

Advice do not watch the spring video tutorials, read the official doc for a specific version of your spring bud
, for example, for the latest version 2.0.M6 . Everything is written for children.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question