Example usage for org.springframework.security.core.authority SimpleGrantedAuthority SimpleGrantedAuthority

List of usage examples for org.springframework.security.core.authority SimpleGrantedAuthority SimpleGrantedAuthority

Introduction

In this page you can find the example usage for org.springframework.security.core.authority SimpleGrantedAuthority SimpleGrantedAuthority.

Prototype

public SimpleGrantedAuthority(String role) 

Source Link

Usage

From source file:org.sharetask.security.UserDetailsByNameService.java

public UserDetails loadUserDetails(T authentication) throws UsernameNotFoundException {
    if (authentication instanceof ClientAuthenticationToken) {
        final CommonProfile profile = (CommonProfile) ((ClientAuthenticationToken) authentication)
                .getUserProfile();//from www. j a  va 2 s. c  om
        UserInformation user = this.userInformationRepository.findByUsername(profile.getEmail());
        if (user == null) {
            return null;
        } else {
            final Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            for (final Role role : user.getRoles()) {
                authorities.add(new SimpleGrantedAuthority(role.name()));
            }
            return buildUserDetails(profile.getEmail(), authorities);
        }
    } else {
        throw new SecurityException("Wrong authentication object");
    }
}

From source file:com.sharmila.hibernatespringsecurity.service.UserService.java

private List<GrantedAuthority> buildUserAuthority(Set<Role> role) {
    Set<GrantedAuthority> setAutho = new HashSet<GrantedAuthority>();

    //build's users authority
    for (Role r : role) {
        setAutho.add(new SimpleGrantedAuthority(r.getRole()));
    }/*w  w w.j a v a2 s  .c o  m*/
    List<GrantedAuthority> result = new ArrayList<>(setAutho);
    return result;
}

From source file:org.terasoluna.gfw.security.web.logging.UserIdMDCPutFilterTest.java

@Test
public void testGetMDCValue() {
    UserIdMDCPutFilter mdcPutFilter = new UserIdMDCPutFilter();

    // expected data
    String userName = "terasoluna@nttd.co.jp";

    // SecurityContextHolder setting start
    securityContext = mock(SecurityContext.class);
    authentication = mock(Authentication.class);
    user = new User(userName, "yyyy", Arrays.asList(new SimpleGrantedAuthority("user")));
    when(authentication.getPrincipal()).thenReturn(user);
    when(securityContext.getAuthentication()).thenReturn(authentication);
    SecurityContextHolder.setContext(securityContext);
    // setting end

    // run/*from   ww w .j  a  va 2 s  . c  o  m*/
    String mdcValueStr = mdcPutFilter.getMDCValue(request, response);

    // assert
    assertThat(mdcValueStr, is(userName));

}

From source file:example.springdata.jpa.security.SecurityIntegrationTests.java

@Before
public void setup() {

    tom = userRepository.save(new User("thomas", "darimont", "tdarimont@example.org"));
    ollie = userRepository.save(new User("oliver", "gierke", "ogierke@example.org"));
    admin = userRepository.save(new User("admin", "admin", "admin@example.org"));

    object1 = businessObjectRepository.save(new BusinessObject("object1", ollie));
    object2 = businessObjectRepository.save(new BusinessObject("object2", ollie));
    object3 = businessObjectRepository.save(new BusinessObject("object3", tom));

    olliAuth = new UsernamePasswordAuthenticationToken(ollie, "x");
    tomAuth = new UsernamePasswordAuthenticationToken(tom, "x");
    adminAuth = new UsernamePasswordAuthenticationToken(admin, "x",
            singleton(new SimpleGrantedAuthority("ROLE_ADMIN")));
}

From source file:com.orange.clara.cloud.servicedbdumper.security.AccessManagerTest.java

private void injectAdmin() {
    when(accessManager.getSecurityContextHolder()).thenReturn(new SecurityContext() {
        @Override/* w w  w  . j  a v  a 2 s.co m*/
        public Authentication getAuthentication() {
            return new Authentication() {
                @Override
                public Collection<? extends GrantedAuthority> getAuthorities() {
                    List<GrantedAuthority> grantedAuthorities = Lists.newArrayList();
                    grantedAuthorities.add(new SimpleGrantedAuthority(AccessManager.AUTHORIZED_AUTHORITY));
                    return grantedAuthorities;
                }

                @Override
                public String getName() {
                    return null;
                }

                @Override
                public Object getCredentials() {
                    return null;
                }

                @Override
                public Object getDetails() {
                    return null;
                }

                @Override
                public Object getPrincipal() {
                    return null;
                }

                @Override
                public boolean isAuthenticated() {
                    return false;
                }

                @Override
                public void setAuthenticated(boolean b) throws IllegalArgumentException {

                }

            };
        }

        @Override
        public void setAuthentication(Authentication authentication) {

        }
    });
}

From source file:org.apigw.appmanagement.revision.ApplicationManagementRevisionListenerTest.java

private SecurityContext createSecurityContext(final String username, String... roles) {
    final List<SimpleGrantedAuthority> authorities = new ArrayList<>();
    for (String role : roles) {
        authorities.add(new SimpleGrantedAuthority(role));
    }/*from w  w w  . jav a  2 s  .  c  o  m*/
    return new SecurityContext() {
        @Override
        public Authentication getAuthentication() {
            return new UsernamePasswordAuthenticationToken(new User(username, "", authorities), null);
        }

        @Override
        public void setAuthentication(Authentication authentication) {

        }
    };
}

From source file:org.softdays.mandy.web.security.MyUserDetailsContextMapper.java

@Override
public UserDetails mapUserFromContext(final DirContextOperations ctx, final String username,
        final Collection<? extends GrantedAuthority> authorities) {
    ResourceDto resource = this.resourceService.findByUid(username);
    if (resource == null) {
        final String lastname = (String) ctx.getObjectAttribute(this.configurationDto.getLdapAttrLastname());
        final String firstname = (String) ctx.getObjectAttribute(this.configurationDto.getLdapAttrFirstname());
        resource = this.resourceService.create(username, lastname, firstname);
    }/*from  w  ww. j  a  v  a  2s  .co m*/
    final String role = resource.getRole();
    final SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role);

    return new MyUser(resource, Arrays.asList(authority));
}

From source file:de.zib.gndms.gndms.security.GridMapUserDetailsService.java

private SimpleGrantedAuthority adminRole() {

    return new SimpleGrantedAuthority("ROLE_ADMIN");
}

From source file:com.evidence.service.UserServiceTest.java

/**
 * Test method for {@link com.tapas.evidence.service.UserServiceImpl#loadUserByUsername(java.lang.String)}.
 *//*w w w. j  a  v a2  s .com*/
@Test
public void testLoadUserByUsername() {
    UserDetails user = userService.loadUserByUsername("admin@evidence.com");
    assertNotNull(user);
    assertTrue(user.getAuthorities().size() == 2);
    ArrayList<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
    list.add(new SimpleGrantedAuthority(Role.ROLE_USER.name()));
    list.add(new SimpleGrantedAuthority(Role.ROLE_ADMINISTRATOR.name()));
    assertTrue(user.getAuthorities().containsAll(list));
}

From source file:com.qpark.eip.core.spring.auth.DatabaseUserProvider.java

/**
 * Map the {@link AuthenticationType} to a {@link User}.
 *
 * @param auth//from   www .j av a2  s . c  o m
 *            the {@link AuthenticationType}.
 * @return the {@link User}.
 */
private User getUser(final AuthenticationType auth) {
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(auth.getGrantedAuthority().size() + 1);
    authorities.add(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
    for (GrantedAuthorityType grantedAuthority : auth.getGrantedAuthority()) {
        authorities.add(new SimpleGrantedAuthority(grantedAuthority.getRoleName()));
    }
    User u = new User(auth.getUserName(), auth.getPassword(), authorities);
    return u;
}