title: Spring XML to JAVA Config
body: <pre><code>&lt;bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/&gt;
  
  &lt;security:http use-expressions="false" entry-point-ref="loginEntryPoint"&gt;
      &lt;security:custom-filter ref="customFormLoginFilter" position="FORM_LOGIN_FILTER"/&gt;      
      &lt;security:logout logout-url="/logout" logout-success-url="/login?logout=true"/&gt;
  
      &lt;security:intercept-url pattern="/appointments/*" access="ROLE_USER"/&gt;
      &lt;security:intercept-url pattern="/schedule/*" access="ROLE_FOO"/&gt;
      &lt;security:intercept-url pattern="/**" access="ROLE_ANONYMOUS, ROLE_USER"/&gt;
  &lt;/security:http&gt;
  
  &lt;bean id="customFormLoginFilter" class="com.fetn.security.CustomAuthenticationFilter"&gt;
      &lt;property name="filterProcessesUrl" value="/login"/&gt;
      &lt;property name="authenticationManager" ref="authenticationManager"/&gt;
      &lt;property name="usernameParameter" value="custom_username"/&gt; 
      &lt;property name="passwordParameter" value="custom_password"/&gt; 
      &lt;property name="authenticationSuccessHandler"&gt; 
          &lt;bean class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"&gt; 
              &lt;property name="defaultTargetUrl" value="/"/&gt; 
          &lt;/bean&gt; 
      &lt;/property&gt; 
      &lt;property name="authenticationFailureHandler"&gt; 
          &lt;bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"&gt; 
              &lt;property name="defaultFailureUrl" value="/login/failure?error=true"/&gt;
          &lt;/bean&gt; 
      &lt;/property&gt; 
  &lt;/bean&gt;
  
  &lt;bean id="loginEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"&gt;
      &lt;constructor-arg value="/login"/&gt;
  &lt;/bean&gt;
  
  &lt;security:authentication-manager alias="authenticationManager"&gt;
      &lt;security:authentication-provider ref="customAuthenticationProvider"/&gt;
  &lt;/security:authentication-manager&gt;
  </code></pre>
  
  <p>I wrote belowJava Config Code but for logout and .antMatchers("/appointments/<em>").access("hasRole('USER')") and  antMatchers("/schedule/</em>").access("hasRole('ADMIN')")</p>
  
  <p>URL always go to /login/failure?error=true</p>
  
  <p>what will be the appropriate java cofig code .Please Help.....</p>
  
  <pre><code>@Configuration
   @EnableWebSecurity
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
  @Autowired
  private AutoUserRepository autoUserRepository;
  
  @Autowired
  private CustomAuthenticationProvider customAuthenticationProvider;
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  
  
          auth.authenticationProvider(customAuthenticationProvider);
  
      }
  
  
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
  
  
  
  
  
       http.authorizeRequests()
  
          .antMatchers("/appointments/*").access("hasRole('USER')").
  
          antMatchers("/schedule/*").access("hasRole('ADMIN')").and().exceptionHandling().authenticationEntryPoint(loginEntryPoint()).and().addFilterBefore(customFormLoginFilter(), UsernamePasswordAuthenticationFilter.class);
  
          http.logout().logoutUrl("/logout")
          .logoutSuccessUrl("/login?logout=true");
  
  
  
  
  
  }
  
  
  @Override
  public void configure(WebSecurity web) throws Exception {
      web.ignoring().antMatchers("/resources/**");
  }
  
  
  
  @Bean
   public DefaultWebSecurityExpressionHandler  defaultWebSecurityExpressionHandler(){
  
       return new DefaultWebSecurityExpressionHandler();
   }
  
  
  
      @Bean
      public LoginUrlAuthenticationEntryPoint loginEntryPoint(){
  
          LoginUrlAuthenticationEntryPoint ent=new LoginUrlAuthenticationEntryPoint("/login");
  
          return ent;
  
  
      }
  
  
      @Bean
      public CustomAuthenticationFilter customFormLoginFilter() throws Exception{
  
          CustomAuthenticationFilter filter=new CustomAuthenticationFilter();
  
          //setting up super class property AbstractAuthenticationProcessingFilter
          filter.setFilterProcessesUrl("/login");//login url
          filter.setAuthenticationManager(authenticationManagerBean());
          filter.setUsernameParameter("custom_username");
          filter.setPasswordParameter("custom_username");
          filter.setAuthenticationSuccessHandler(savedRequestAwareAuthenticationSuccessHandler());
          filter.setAuthenticationFailureHandler(simpleUrlAuthenticationFailureHandler());
  
  
          return filter;
  
      }
  
  
  
      @Bean
      public SavedRequestAwareAuthenticationSuccessHandler savedRequestAwareAuthenticationSuccessHandler(){
  
          SavedRequestAwareAuthenticationSuccessHandler surl=new SavedRequestAwareAuthenticationSuccessHandler();
          surl.setDefaultTargetUrl("/");//url after seuuces login
  
          return surl;
      }
  
      @Bean
      SimpleUrlAuthenticationFailureHandler simpleUrlAuthenticationFailureHandler(){
          SimpleUrlAuthenticationFailureHandler faillure=new SimpleUrlAuthenticationFailureHandler();
          faillure.setDefaultFailureUrl("/login/failure?error=true");
  
  
          return  faillure;
  
      }
  
  
      @Bean
         @Override
         public AuthenticationManager authenticationManagerBean() throws Exception {
             return super.authenticationManagerBean();
         }
  </code></pre>
  
  <p>}</p>