@Override public boolean equals(Object obj) { if (!super.equals(obj)) { return false; } if (obj instanceof AnonymousAuthenticationToken) { AnonymousAuthenticationToken test = (AnonymousAuthenticationToken) obj; if (this.getKeyHash() != test.getKeyHash()) { return false; } return true; } return false; }
@Override public boolean equals(Object obj) { if (!super.equals(obj)) { return false; } if (obj instanceof AnonymousAuthenticationToken) { AnonymousAuthenticationToken test = (AnonymousAuthenticationToken) obj; if (this.getKeyHash() != test.getKeyHash()) { return false; } return true; } return false; }
public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } if (this.key.hashCode() != ((AnonymousAuthenticationToken) authentication) .getKeyHash()) { throw new BadCredentialsException( messages.getMessage("AnonymousAuthenticationProvider.incorrectKey", "The presented AnonymousAuthenticationToken does not contain the expected key")); } return authentication; }
public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } if (this.key.hashCode() != ((AnonymousAuthenticationToken) authentication) .getKeyHash()) { throw new BadCredentialsException( messages.getMessage("AnonymousAuthenticationProvider.incorrectKey", "The presented AnonymousAuthenticationToken does not contain the expected key")); } return authentication; }
@GetMapping("/customKey") public String customKey() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if ( authentication != null && authentication instanceof AnonymousAuthenticationToken ) { return String.valueOf( ((AnonymousAuthenticationToken) authentication).getKeyHash()); } return null; } }
private void assertAnonymous() { Authentication currentAuthentication = SecurityContextHolder.getContext() .getAuthentication(); assertThat(currentAuthentication) .isInstanceOf(AnonymousAuthenticationToken.class); AnonymousAuthenticationToken anonymous = (AnonymousAuthenticationToken) currentAuthentication; assertThat(anonymous.getName()).isEqualTo(expectedAnonymous.getName()); assertThat(anonymous.getAuthorities()).containsOnlyElementsOf( expectedAnonymous.getAuthorities()); assertThat(anonymous.getKeyHash()).isEqualTo(expectedAnonymous.getKeyHash()); } }
@Test public void deserializeAnonymousAuthenticationTokenTest() throws IOException { AnonymousAuthenticationToken token = mapper .readValue(ANONYMOUS_JSON, AnonymousAuthenticationToken.class); assertThat(token).isNotNull(); assertThat(token.getKeyHash()).isEqualTo(HASH_KEY.hashCode()); assertThat(token.getAuthorities()).isNotNull().hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER")); }
@Test public void testGetters() { AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("key", "Test", ROLES_12); assertThat(token.getKeyHash()).isEqualTo("key".hashCode()); assertThat(token.getPrincipal()).isEqualTo("Test"); assertThat(token.getCredentials()).isEqualTo(""); assertThat(AuthorityUtils.authorityListToSet(token.getAuthorities())).contains( "ROLE_ONE", "ROLE_TWO"); assertThat(token.isAuthenticated()).isTrue(); }
@Override public int getKeyHash() { return anonymousAuthenticationToken.getKeyHash(); }
public boolean equals(Object obj) { if (!super.equals(obj)) { return false; } if (obj instanceof AnonymousAuthenticationToken) { AnonymousAuthenticationToken test = (AnonymousAuthenticationToken) obj; if (this.getKeyHash() != test.getKeyHash()) { return false; } return true; } return false; }
@Override public boolean equals(Object obj) { if (!super.equals(obj)) { return false; } if (obj instanceof AnonymousAuthenticationToken) { AnonymousAuthenticationToken test = (AnonymousAuthenticationToken) obj; if (this.getKeyHash() != test.getKeyHash()) { return false; } return true; } return false; }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (!authenticatedAccess && SecurityContextHolder.getContext().getAuthentication() != null && SecurityContextHolder.getContext().getAuthentication() instanceof AnonymousAuthenticationToken) { AnonymousAuthenticationToken anonymousToken = (AnonymousAuthenticationToken)SecurityContextHolder.getContext().getAuthentication(); AnonymousAuthenticationToken newAnonymousToken = new AnonymousAuthenticationToken(Integer.toString(anonymousToken.getKeyHash()),anonymousToken.getPrincipal(),AuthorityUtils.createAuthorityList("ROLE_USER")); SecurityContextHolder.getContext().setAuthentication(newAnonymousToken); } chain.doFilter(request, response); }
public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } if (this.key.hashCode() != ((AnonymousAuthenticationToken) authentication).getKeyHash()) { throw new BadCredentialsException(messages.getMessage("AnonymousAuthenticationProvider.incorrectKey", "The presented AnonymousAuthenticationToken does not contain the expected key")); } return authentication; }
public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } if (this.key.hashCode() != ((AnonymousAuthenticationToken) authentication) .getKeyHash()) { throw new BadCredentialsException( messages.getMessage("AnonymousAuthenticationProvider.incorrectKey", "The presented AnonymousAuthenticationToken does not contain the expected key")); } return authentication; }