Skip to content

Commit 4d871a6

Browse files
Sumanth Kumar Morazabil
authored andcommitted
Add cache control and pragma header to login page (#2562)
1 parent 03cdae2 commit 4d871a6

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed

server/src/com/thoughtworks/go/server/controller/AuthorizationController.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,44 @@
1818

1919
import com.thoughtworks.go.i18n.Localizer;
2020
import com.thoughtworks.go.plugin.access.authentication.AuthenticationPluginRegistry;
21-
import com.thoughtworks.go.server.service.SecurityService;
2221
import org.springframework.beans.factory.annotation.Autowired;
2322
import org.springframework.stereotype.Controller;
2423
import org.springframework.web.bind.annotation.RequestMapping;
2524
import org.springframework.web.bind.annotation.RequestMethod;
2625
import org.springframework.web.bind.annotation.RequestParam;
2726
import org.springframework.web.servlet.ModelAndView;
2827

29-
import javax.servlet.http.HttpServletRequest;
3028
import javax.servlet.http.HttpServletResponse;
3129
import java.io.IOException;
3230
import java.util.HashMap;
3331

3432
@Controller
3533
public class AuthorizationController {
3634
private final Localizer localizer;
37-
private final SecurityService securityService;
3835
private AuthenticationPluginRegistry authenticationPluginRegistry;
3936

4037
@Autowired
41-
public AuthorizationController(Localizer localizer, SecurityService securityService,
42-
AuthenticationPluginRegistry authenticationPluginRegistry) {
38+
public AuthorizationController(Localizer localizer, AuthenticationPluginRegistry authenticationPluginRegistry) {
4339
this.localizer = localizer;
44-
this.securityService = securityService;
4540
this.authenticationPluginRegistry = authenticationPluginRegistry;
4641
}
4742

4843
@RequestMapping(value = "/auth/login", method = RequestMethod.GET)
4944
public ModelAndView login(@RequestParam(value = "login_error", required = false) Boolean loginError,
50-
HttpServletRequest request, HttpServletResponse response) throws IOException {
45+
HttpServletResponse response) throws IOException {
46+
47+
response.setHeader("Cache-Control", "no-cache, must-revalidate, no-store");
48+
response.setHeader("Pragma", "no-cache");
49+
5150
HashMap model = new HashMap();
5251
model.put("login_error", loginError);
5352
model.put("l", localizer);
5453
model.put("authentication_plugin_registry", authenticationPluginRegistry);
5554
return new ModelAndView("auth/login", model);
5655
}
5756

58-
5957
@RequestMapping(value = "/auth/security_check", method = RequestMethod.POST)
60-
public ModelAndView securityCheckHandlerWhenAuthenticationProcessingFilterIsOff(HttpServletRequest request,
61-
HttpServletResponse response)
58+
public ModelAndView securityCheckHandlerWhenAuthenticationProcessingFilterIsOff(HttpServletResponse response)
6259
throws IOException {
6360
response.sendRedirect("/go");
6461
return null;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2016 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.thoughtworks.go.server.controller;
18+
19+
import com.thoughtworks.go.i18n.Localizer;
20+
import com.thoughtworks.go.plugin.access.authentication.AuthenticationPluginRegistry;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.springframework.mock.web.MockHttpServletResponse;
24+
import org.springframework.ui.ModelMap;
25+
import org.springframework.web.servlet.ModelAndView;
26+
27+
import java.util.Map;
28+
29+
import static org.hamcrest.core.Is.is;
30+
import static org.junit.Assert.assertThat;
31+
import static org.mockito.Mockito.mock;
32+
33+
public class AuthorizationControllerTest {
34+
35+
Localizer localizer;
36+
AuthenticationPluginRegistry authenticationPluginRegistry;
37+
38+
private AuthorizationController authorizationController;
39+
private MockHttpServletResponse response;
40+
41+
@Before
42+
public void setUp() throws Exception {
43+
authenticationPluginRegistry = mock(AuthenticationPluginRegistry.class);
44+
localizer = mock(Localizer.class);
45+
46+
authorizationController = new AuthorizationController(localizer, authenticationPluginRegistry);
47+
response = new MockHttpServletResponse();
48+
}
49+
50+
@Test
51+
public void shouldAddCacheControlHeaderToTheResponse() throws Exception {
52+
authorizationController.login(false, response);
53+
54+
assertThat(response.getHeader("Cache-Control"), is("no-cache, must-revalidate, no-store"));
55+
}
56+
57+
@Test
58+
public void shouldAddPragmaHeaderToTheResponse() throws Exception {
59+
authorizationController.login(false, response);
60+
61+
assertThat(response.getHeader("Pragma"), is("no-cache"));
62+
}
63+
64+
@Test
65+
public void shouldSetModel() throws Exception {
66+
ModelAndView responseModel = authorizationController.login(false, response);
67+
68+
Map<String, Object> modelMap = new ModelMap() {{
69+
put("login_error", false);
70+
put("l", localizer);
71+
put("authentication_plugin_registry", authenticationPluginRegistry);
72+
}};
73+
74+
Map<String, Object> responseModelMap = responseModel.getModel();
75+
assertThat(responseModelMap, is(modelMap));
76+
}
77+
78+
@Test
79+
public void shouldRedirectToGo() throws Exception {
80+
authorizationController.securityCheckHandlerWhenAuthenticationProcessingFilterIsOff(response);
81+
82+
assertThat(response.getRedirectedUrl(), is("/go"));
83+
}
84+
}

0 commit comments

Comments
 (0)