How to convert spring security xml to annotation
<p>I have a application use spring security configure xml integrate with spring MVC.</p>

<p>My spring security configure xml:</p>

<pre><code>&lt;http pattern="/admin/**" authentication-manager-ref="adminAuthenticationManager"&gt;
    &lt;intercept-url pattern="/" access="permitAll" /&gt;
    &lt;form-login login-page="/login" username-parameter="userEmail"
        login-processing-url="/admin/j_spring_security_check"
        authentication-success-handler-ref="customAuthenticationSuccessHandler"
        password-parameter="password" authentication-failure-url="/admin/login?error" /&gt;
    &lt;logout logout-url="/admin/j_spring_security_logout"
        logout-success-url="/admin/login?logout" /&gt;
    &lt;remember-me key="uniqueAndSecret" user-service-ref="customUserDetailsService"
        token-validity-seconds="7776000" /&gt;
    &lt;csrf disabled="true" /&gt;
&lt;/http&gt;
&lt;http authentication-manager-ref="userAuthenticationManager"&gt;
    &lt;intercept-url pattern="/" access="permitAll" /&gt;
    &lt;form-login login-page="/login" username-parameter="userEmail"
        login-processing-url="/j_spring_security_check" password-parameter="password"
        authentication-success-handler-ref="customAuthenticationSuccessHandler"
        authentication-failure-url="/login?error" /&gt;
    &lt;logout logout-url="/j_spring_security_logout"
        logout-success-url="/login?logout" /&gt;
    &lt;remember-me key="uniqueAndSecret" user-service-ref="customAdminDetailsService"
        token-validity-seconds="7776000" /&gt;
    &lt;csrf disabled="true" /&gt;
&lt;/http&gt;

&lt;authentication-manager erase-credentials="false"
    id="userAuthenticationManager"&gt;
    &lt;authentication-provider user-service-ref="customUserDetailsService" /&gt;
&lt;/authentication-manager&gt;
&lt;authentication-manager erase-credentials="false"
    id="adminAuthenticationManager"&gt;
    &lt;authentication-provider user-service-ref="customAdminDetailsService" /&gt;
&lt;/authentication-manager&gt;

&lt;beans:bean id="customUserDetailsService"
    class="com.service.impl.UserDetailServiceImpl" /&gt;
&lt;beans:bean id="customAdminDetailsService"
    class="com.service.impl.AdminDetailServiceImpl" /&gt;
</code></pre>

<p>My custom UserServiceDetailImpl file:</p>

<pre><code>@Service 
public class UserDetailServiceImpl implements UserDetailsService {
@Autowired
private UserService userService;

@Override
public UserDetails loadUserByUsername(String email) {
    try {
        User user = userService.getUserByEmail(email);
        if (user != null) {
            return new org.springframework.security.core.userdetails.User(user.getUserEmail(), user.getPassword(),
                    true, true, true, true, getGrantedAuthorities(user));
        } else {
            throw new UsernameNotFoundException(email);
        }
    } catch (Exception e) {
        throw new UsernameNotFoundException(email);
    }
}

private List&lt;GrantedAuthority&gt; getGrantedAuthorities(Object generalUser) {
    List&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;GrantedAuthority&gt;();
    if (generalUser instanceof User) {
        authorities.add(new SimpleGrantedAuthority(CommonConstant.Role.USER.toString()));
    }
    return authorities;
    }
}
</code></pre>

<p>Now How can I change it to spring configure by annotation? </p>

<p>My WebsercurityConfig file when I changed to use annotation in spring security:</p>

<pre><code>@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
UserDetailServiceImpl userDetailService;

@Autowired
AdminDetailServiceImpl adminDetailService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailService);
    auth.userDetailsService(adminDetailService);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll()
            .and().formLogin().loginPage("/login")
            .failureUrl("/login?error").loginProcessingUrl("/j_spring_security_check")
            .successHandler(loginSuccessHanlder).failureUrl("/login?error=true")
            .usernameParameter("userEmail").passwordParameter("password")
            .and().logout().logoutUrl("/j_spring_security_logout").logoutSuccessUrl("/login?logout")
            .and().userDetailsService(userDetailService)
            .rememberMe().key("uniqueAndSecret")
            .tokenValiditySeconds(33343)
            .and()
            .authorizeRequests().antMatchers("/admin/**").permitAll()
            .and().formLogin().loginPage("/login")
            .failureUrl("/admin/login?error").loginProcessingUrl("/admin/j_spring_security_check")
            .successHandler(loginSuccessHanlder).failureUrl("/admin/login?error=true")
            .usernameParameter("userEmail").passwordParameter("password")
            .and().logout().logoutUrl("/admin/j_spring_security_logout").logoutSuccessUrl("/admin/login?logout")
            .and().userDetailsService(userDetailService)
            .rememberMe().key("uniqueAndSecret")
            .tokenValiditySeconds(33343);
     }
}
</code></pre>