List of usage examples for org.springframework.security.authentication UsernamePasswordAuthenticationToken UsernamePasswordAuthenticationToken
public UsernamePasswordAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities)
AuthenticationManager
or AuthenticationProvider
implementations that are satisfied with producing a trusted (i.e. From source file:org.apache.cxf.fediz.service.idp.service.jpa.ConfigServiceJPA.java
@Override public Idp getIDP(String realm) { Authentication currentAuthentication = SecurityContextHolder.getContext().getAuthentication(); try {//from w w w .jav a 2s .co m final Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); if (realm == null || realm.length() == 0) { authorities.add(new SimpleGrantedAuthority("IDP_LIST")); UsernamePasswordAuthenticationToken technicalUser = new UsernamePasswordAuthenticationToken( "IDP_TEST", "N.A", authorities); SecurityContextHolder.getContext().setAuthentication(technicalUser); return idpService.getIdps(0, 1, Arrays.asList("all"), null).getIdps().iterator().next(); } else { authorities.add(new SimpleGrantedAuthority("IDP_READ")); UsernamePasswordAuthenticationToken technicalUser = new UsernamePasswordAuthenticationToken( "IDP_TEST", "N.A", authorities); SecurityContextHolder.getContext().setAuthentication(technicalUser); return idpService.getIdp(realm, Arrays.asList("all")); } } finally { SecurityContextHolder.getContext().setAuthentication(currentAuthentication); LOG.info("Old Spring security context restored"); } }
From source file:com.wisemapping.security.AuthenticationProvider.java
@Override() public Authentication authenticate(@NotNull final Authentication auth) throws AuthenticationException { // All your user authentication needs final String email = auth.getName(); final UserDetails userDetails = getUserDetailsService().loadUserByUsername(email); final User user = userDetails.getUser(); final String credentials = (String) auth.getCredentials(); if (user == null || credentials == null || !encoder.isPasswordValid(user.getPassword(), credentials, null)) { throw new BadCredentialsException("Username/Password does not match for " + auth.getPrincipal()); }//ww w. jav a 2s. co m userDetailsService.getUserService().auditLogin(user); return new UsernamePasswordAuthenticationToken(userDetails, credentials, userDetails.getAuthorities()); }
From source file:br.com.sicva.seguranca.ProvedorAutenticacao.java
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String name = authentication.getName(); String password = authentication.getCredentials().toString(); UsuariosDao usuariosDao = new UsuariosDao(); Usuarios usuario = usuariosDao.PesquisarUsuario(name); if (usuario == null || !usuario.getUsuariosCpf().equalsIgnoreCase(name)) { throw new BadCredentialsException("Username not found."); }/* w w w . j a va 2 s .c om*/ if (!GenerateMD5.generate(password).equals(usuario.getUsuarioSenha())) { throw new LockedException("Wrong password."); } if (!usuario.getUsuarioAtivo()) { throw new DisabledException("User is disable"); } List<GrantedAuthority> funcoes = new ArrayList<>(); funcoes.add(new SimpleGrantedAuthority("ROLE_" + usuario.getFuncao().getFuncaoDescricao())); Collection<? extends GrantedAuthority> authorities = funcoes; return new UsernamePasswordAuthenticationToken(name, password, authorities); }
From source file:com.faujnet.signin.adapter.SimpleSignInAdapter.java
@Override public String signIn(String localUserId, Connection<?> connection, NativeWebRequest request) { UserAccount account = userAdminService.loadUserByUserId(localUserId); SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(localUserId, account.getPassword(), account.getAuthorities())); Authentication auth = SecurityContextHolder.getContext().getAuthentication(); ((AbstractAuthenticationToken) auth).setDetails(account); System.out.println("USERID = " + localUserId); return extractOriginalUrl(request); }
From source file:org.socialsignin.springsocial.security.signin.SpringSocialSecurityAuthenticationFactory.java
protected Authentication createNewAuthentication(String userId, String password, Collection<? extends GrantedAuthority> authorities) { return new UsernamePasswordAuthenticationToken(userId, password, authorities); }
From source file:com.rockagen.gnext.service.spring.security.extension.TokenAuthenticationFilter.java
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; String token = req.getHeader(tokenName); if (CommUtil.isNotBlank(token)) { //Validate token UserDetails user = exTokenAuthentication.getUserDetailsFromToken(token); if (user != null) { // authenticated UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( user.getUsername(), user.getPassword(), user.getAuthorities()); authentication.setDetails(new ExWebAuthenticationDetails(req)); SecurityContextHolder.getContext().setAuthentication(authentication); }/*w ww . j a v a 2s . c o m*/ } chain.doFilter(request, response); }
From source file:org.openlmis.fulfillment.security.CustomUserAuthenticationConverter.java
/** * {@inheritDoc}./*from w ww.j ava2 s . c om*/ */ public Authentication extractAuthentication(Map<String, ?> map) { if (map.containsKey(REFERENCE_DATA_USER_ID)) { UUID principal = UUID.fromString((String) map.get(REFERENCE_DATA_USER_ID)); Collection<? extends GrantedAuthority> authorities = getAuthorities(map); return new UsernamePasswordAuthenticationToken(principal, "N/A", authorities); } return null; }
From source file:org.ff4j.security.test.FlipSecurityTests.java
@Before public void setUp() throws Exception { securityCtx = SecurityContextHolder.getContext(); // Init SpringSecurity Context SecurityContext context = new SecurityContextImpl(); List<GrantedAuthority> listOfRoles = new ArrayList<GrantedAuthority>(); listOfRoles.add(new SimpleGrantedAuthority("ROLE_USER")); User u1 = new User("user1", "user1", true, true, true, true, listOfRoles); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(u1.getUsername(), u1.getPassword(), u1.getAuthorities()); token.setDetails(u1);/*from www . j a va 2 s .c o m*/ context.setAuthentication(token); SecurityContextHolder.setContext(context); // <-- ff4j = new FF4j("test-ff4j-security-spring.xml"); ff4j.setAuthorizationsManager(new SpringSecurityAuthorisationManager()); }