List of usage examples for org.springframework.security.authentication UsernamePasswordAuthenticationToken UsernamePasswordAuthenticationToken
public UsernamePasswordAuthenticationToken(Object principal, Object credentials)
UsernamePasswordAuthenticationToken
, as the #isAuthenticated() will return false
. From source file:io.github.autsia.crowly.controllers.rest.AuthenticationController.java
@RequestMapping(value = "/login", method = RequestMethod.POST) public ResponseEntity<String> login(@RequestBody CrowlyUser user, HttpServletResponse response) { try {//from www . ja v a 2 s. c o m Authentication request = new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPassword()); Authentication result = authenticationManager.authenticate(request); SecurityContextHolder.getContext().setAuthentication(result); return new ResponseEntity<>(HttpStatus.OK); } catch (AuthenticationException e) { logger.warn("Failed login attempt for username: " + e.getMessage()); return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } }
From source file:org.parancoe.plugins.securityevolution.AuthenticationManagerTest.java
public void testAuthenticateWithBadCredentials() { authentication = new UsernamePasswordAuthenticationToken("parancoe", "wrongPassword"); try {/*from ww w .j ava2 s . co m*/ authenticationManager.authenticate(authentication); fail("Provided password is wrong"); } catch (AuthenticationException e) { assertTrue(e instanceof BadCredentialsException); } }
From source file:com.springsource.greenhouse.settings.SettingsControllerTest.java
@Before public void setup() { db = new GreenhouseTestDatabaseBuilder().member().connectedApp().testData(getClass()).getDatabase(); tokenStore = new JdbcTokenStore(db); jdbcTemplate = new JdbcTemplate(db); controller = new SettingsController(tokenStore, jdbcTemplate); AuthorizationRequest authorizationRequest = new DefaultAuthorizationRequest( "a08318eb478a1ee31f69a55276f3af64", Arrays.asList("read", "write")); Authentication userAuthentication = new UsernamePasswordAuthenticationToken("1", "whateveryouwantittobe"); tokenStore.storeAccessToken(new DefaultOAuth2AccessToken("authme"), new OAuth2Authentication(authorizationRequest, userAuthentication)); assertEquals(1, tokenStore.findTokensByUserName("1").size()); }
From source file:security.LoginPasswordSecurityService.java
/** * {@inheritDoc}/*www .j av a 2s . c om*/ */ public boolean authenticate(String username, String password) { Authentication aut = new UsernamePasswordAuthenticationToken(username, password); try { aut = authenticationManager.authenticate(aut); SecurityContextHolder.getContext().setAuthentication(aut); } catch (Exception e) { e.printStackTrace(); return false; } return aut.isAuthenticated(); }
From source file:uk.org.openeyes.oink.security.HttpBasicPreauthProcessor.java
public void extractAuthenticationDetailsFromHttp(Exchange exchange) throws SecurityException { // get the username and password from the HTTP header // http://en.wikipedia.org/wiki/Basic_access_authentication String authorizationHeader = exchange.getIn().getHeader("Authorization", String.class); if (authorizationHeader == null) { throw new SecurityException("No HttpBasic Authorization Header was found in the request"); }//from w ww. j a v a2s . co m String basicPrefix = "Basic "; String userPassword = authorizationHeader.substring(basicPrefix.length()); byte[] header = Base64.decodeBase64(userPassword.getBytes()); if (header == null) { throw new SecurityException("Invalid Http Basic Authorization Header found in the request"); } String userpass = new String(header); String[] tokens = userpass.split(":"); // create an Authentication object UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(tokens[0], tokens[1]); // wrap it in a Subject Subject subject = new Subject(); subject.getPrincipals().add(authToken); // place the Subject in the In message exchange.getIn().setHeader(Exchange.AUTHENTICATION, subject); logger.debug("Found HttpBasic Authentication header"); // Spring security will intercept this and authenticate }
From source file:ch.silviowangler.dox.AbstractIntegrationTest.java
protected void loginAs(String username) { SecurityContextHolder.clearContext(); SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken( new User(username, "velo", new ArrayList<GrantedAuthority>()), "velo")); }
From source file:org.ngrinder.security.NGrinderAuthenticationProviderTest.java
@Test public void testAdditionalAuthenticationChecks() { UserDetails user = userDetailService.loadUserByUsername(getTestUser().getUserId()); //remove authentication temporally Authentication oriAuth = SecurityContextHolder.getContext().getAuthentication(); SecurityContextImpl context = new SecurityContextImpl(); SecurityContextHolder.setContext(context); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("admin", null); try {// w w w . j a va 2s .c o m provider.additionalAuthenticationChecks(user, token); assertTrue(false); } catch (BadCredentialsException e) { assertTrue(true); } token = new UsernamePasswordAuthenticationToken("TEST_USER", "123"); provider.additionalAuthenticationChecks(user, token); context.setAuthentication(oriAuth); }
From source file:eu.openanalytics.rsb.security.JmxSecurityAuthenticator.java
@Override public Subject authenticate(final Object credentials) { try {/*from w w w . j a v a 2s .c om*/ final String[] info = (String[]) credentials; final Authentication authentication = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(info[0], info[1])); final User authenticatedUser = (User) authentication.getPrincipal(); if ((isRsbAdminPrincipal(authenticatedUser)) || (isRsbAdminRole(authenticatedUser))) { final Subject s = new Subject(); s.getPrincipals().add(new JMXPrincipal(authentication.getName())); return s; } else { throw new SecurityException("Authenticated user " + authenticatedUser + " is not an RSB admin"); } } catch (final Exception e) { LOGGER.error("Error when trying to authenticate JMX credentials of type: " + credentials.getClass(), e); throw new SecurityException(e); } }
From source file:hsa.awp.scire.controller.TestController.java
@Before public void setup() { logger = LoggerFactory.getLogger(this.getClass()); SecurityContextHolder.getContext()// www .ja v a2 s . co m .setAuthentication(new UsernamePasswordAuthenticationToken("admin", "password")); controller.setTimerInterval(1000); }
From source file:org.key2gym.client.ContextManager.java
public synchronized void login(String username, String password) throws ValidationException { if (shadowAuthentication != null) { throw new IllegalStateException("There is already a primary and a shadow authentication."); }//ww w . j a v a 2 s . com logger.debug("Log in attempt: " + username); Authentication request = new UsernamePasswordAuthenticationToken(username, password); Authentication result; try { result = Main.getContext().getBean("authenticationManager", AuthenticationManager.class) .authenticate(request); } catch (BadCredentialsException ex) { Logger.getLogger(ContextManager.class).info("Authentication failed for the user: " + username, ex); throw new ValidationException(ResourcesManager.getStrings().getString("Message.LoggingInFailed")); } if (authentication == null) { authentication = result; } else { shadowAuthentication = authentication; authentication = result; } SecurityContextHolder.getContext().setAuthentication(authentication); setChanged(); notifyObservers(); }