Logout with Spring Security with Java configuration

I am using Spring Security 4.0.2.RELEASE with Spring 4.2.0.RELEASE.

I am unable to create a logout link (I maen what must be the value of the href attribute).

Consider :

Configuring DelegatingFilterProxy in Java with WebApplicationInitializer:

public class SecurityWebInitializer
    extends AbstractSecurityWebApplicationInitializer {

}

Simple configuration class to enable web security for Spring MVC

@Configuration
@EnableWebSecurity
public class SecurityConfig
    extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.formLogin().and()
            .authorizeRequests()
            .antMatchers("/spitter/").authenticated()   
            .antMatchers(HttpMethod.GET, "/spitter/register").authenticated().and()

            .logout().deleteCookies("remove")
            .invalidateHttpSession(true).logoutUrl("/logout")
            .logoutSuccessUrl("/");

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {

        auth.inMemoryAuthentication().withUser("user").password("password")
            .roles("USER").and().withUser("admin").password("password")
            .roles("USER", "ADMIN");
    }

}

Controller:

@Controller
@RequestMapping(value = "/spitter")
public class SpittrController {

    private SpittleRepository spittleRepository;

    @Autowired
    public SpittrController(SpittleRepository spittleRepository) {

        this.spittleRepository = spittleRepository;
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String showRegistrationForm() {

        return "registerForm";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String processingRegistration(@Valid Spitter spitter, Errors errors) {

        if (errors.hasErrors()) {
            return "registerForm";
        }

        spittleRepository.save(spitter);
        return "redirect:/spitter/" + spitter.getUserName();

    }

    @RequestMapping(value = "/{username}", method = RequestMethod.GET)
    public String showSpitterProfile(@PathVariable("username") String username,
                                     Model model) {

        Spitter spitter = spittleRepository.findByUsername(username);
        if(spitter != null){
            model.addAttribute(spitter);
        }

        return "profile";
    }
}

registerForm.jsp:

<form method="post">
        <table>
            <tr>
                <td>First Name:</td>
                <td><input type="text" name="firstName" /></td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td><input type="text" name="lastName" /></td>
            </tr>
            <tr>
                <td>User Name:</td>
                <td><input type="text" name="userName" /></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="password" /></td>
            </tr>
            <tr>

                <td><input type="submit" value="Register" /></td>
            </tr>
        </table>
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
    </form>

After submission of registerForm.jsp, the profile.jsp is shown to the user:

profile.jsp:

<body>
    <h1>Hello world!</h1>

    <p>The time on the server is ${serverTime}.</p>

    <h1>Your Profile</h1>
    <h1><a href="/logout">Logout</a></h1>


    <table>
        <tr>
            <td>First Name:</td>
            <td><c:out value="${spitter.firstName}" /></td>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td><c:out value="${spitter.lastName}" /></td>
        </tr>
        <tr>
            <td>User Name:</td>
            <td><c:out value="${spitter.userName}" /></td>
        </tr>
    </table>    
</body>

When I hit

http://localhost:8080/web/spitter/register

I am redirected to the login page. After login and submitting the form, the profile.jsp is shown in which I have included a Logout link. On clicking that, HTTP 404 comes up.

I have gone through Spring Security docs, but they have taken thymeleaf into consideration. My is a simple JSP page.

Furthermore, I have also considered taking this into account,

By default POST request is required to the logout url. To perform logout on GET request you need:

http .logout() .logoutRequestMatcher(new AntPathRequestMatcher("/logout"));

1: http://docs.spring.io/spring-security/site/docs/3.2.x/guides/hellomvc.html

Any suggestions?

Upvotes: 1

Views: 1396

Answers (1)

Balwinder Singh
Balwinder Singh

Reputation: 2282

Update the your code in profile.jsp as

<h1><a href="#" onclick="javascript:logoutForm.submit();">logout</a></h1>

        <c:url var="logoutUrl" value="/logout" />
        <form action="${logoutUrl}" method="post" id="logoutForm">
            <input type="hidden" name="${_csrf.parameterName}"
                value="${_csrf.token}" />
        </form>

Upvotes: 2

Related Questions