testing Spring-Security -
I have many controllers in my Spring-boot (1.1.4.RELEASE) / Spring-Security application that I want However, running some integration tests, I do not know how the authentication is handled to make the request.
This is my test:
@ContextConfiguration (classes = OFAC, loader = SpringApplicationContextLoader) @ Transaction @WebAppConfiguration @IntegrationTest Class AdminControllerIntegrationTest Specification {def adminUrl = "http: / / / Localhost: 9001 / admin "@Autowired Personal AdminController AdminController Expands; DEF "Testing to retrieve users from DB table" () {def model = Fake (Model) RestTemplate restTemplate = New TestRestTemplate () when: DEF result = restTemplate.getForEntity (adminUrl, String.class, model) Then: Results ! = Null}
This is my security configuration:
@configuration @ EnableWebMvc security public class ApplicationSecurity WebSecurityConfigurerAdapter Extended {@Autowired Private CustomUserDetailsService customUserDetailsService; @ Override Protected Zero Configuration (HTTPPquery HTF) throws an exception {http .authorizeRequests () .Atmatateurs ("/"). Permit All () .Autam Matters ("/ Resources / **") .Permat all () .Atmachar ( "/ Css / **"). All Published (.) Atom Matches ("/ Libs / **") .Permate All (); Http .formlogin (). FailureUrl ("/ login / error") .defaultSuccessUrl ("/") .loginPage ("/ login") .permitAll () .and () .logout (). LogoutRequestMatcher (new AntPathRequestMatcher ("/ logout")) .logoutSuccessUrl ("/") .permitAll (); Http .sessionManagement () .max session (1) .expiredUrl ( "/ have went log ends") .maxSessionsPreventsLogin (true) And () .sessionCreationPolicy (Satrkriya policy. EV_REQUIRED) .invalidSessionUrl ( "/"); Http. AuthorizeRequests (). AnyRequest (). Authenticated (); } @ Configure Override Protected Zero (Authentication Manager Account) Exception {Password Encoder Encoder = New BCRP Password Encoder (); Auth.userDetailsService (customUserDetailsService) .passwordEncoder (Encoder); }
When I debug that code, the result I will get is the login HTML I believe that this means that authentication failed (and since I have not defined the user / password anywhere) and my request is being redirected to the login page.
I was searching for a good way to run the Unity Test in this way, but I did not find a good solution. If anybody has any example on how to handle it, then I hope that you
help me You do not have any choice to test kind of, but to post for the entry and to remove the session cookie, so that you can send it with a request to actually check it. Something like this:
Private string login andgraakoku () {reactants < String & gt; Page = serverringgateforstring ("/ sparklr2 / login.jsp"); String cookie = page.getHeaders (). GetFirst ("set-cookie"); Matcher matcher = Pattern.compile ("(? S). * Name = \" _ csrf \ ". * Value = \" ([^ \ "] +.)".). Mattress (page .getBody ()); Multivellomap & lt; String, string & gt; FormData; FormData = New Link Value Value Validation & lt; String, string & gt; (); FormData.add ("j_username", "marissa"); FormData.add ("j_password", "Koala"); If (matcher.matches ()) {formData.add ("_ csrf", matcher.group (1)); } String space = "/sparklr2/login.do"; HTTP header header = new HTTP header (); Headers.set ("cookie", cookie); Headers.setAccept (Arrays.asList (MediaType.TEXT_HTML)); ResponseEntity & LT; Zero & gt; Results = serverRunning.postForStatus (location, header, form data); AssertEquals (HttpStatus.FOUND, results .getStatusCode ()); Cookie = result.getHeaders (). GetFirst ("set-cookie"); AssertNotNull ("Expected Cookie" + result.getHeaders (), Cookies); Return Cookie; }
(Taken from.)
Comments
Post a Comment