use of org.springframework.security.core.authority.SimpleGrantedAuthority in project spring-security by spring-projects.
the class DefaultLdapAuthoritiesPopulator method getGroupMembershipRoles.
public Set<GrantedAuthority> getGroupMembershipRoles(String userDn, String username) {
if (getGroupSearchBase() == null) {
return new HashSet<GrantedAuthority>();
}
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
if (logger.isDebugEnabled()) {
logger.debug("Searching for roles for user '" + username + "', DN = " + "'" + userDn + "', with filter " + this.groupSearchFilter + " in search base '" + getGroupSearchBase() + "'");
}
Set<String> userRoles = getLdapTemplate().searchForSingleAttributeValues(getGroupSearchBase(), this.groupSearchFilter, new String[] { userDn, username }, this.groupRoleAttribute);
if (logger.isDebugEnabled()) {
logger.debug("Roles from search: " + userRoles);
}
for (String role : userRoles) {
if (this.convertToUpperCase) {
role = role.toUpperCase();
}
authorities.add(new SimpleGrantedAuthority(this.rolePrefix + role));
}
return authorities;
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project SI2016_TIM6 by SoftverInzenjeringETFSA.
the class TokenAuthenticationService method getAuthentication.
public static Authentication getAuthentication(HttpServletRequest request) {
ServletContext servletContext = request.getServletContext();
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
studentRepository = webApplicationContext.getBean(StudentRepository.class);
String token = request.getHeader(HEADER_STRING);
if (token != null) {
// parse the token.
String user = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token.replace(TOKEN_PREFIX, "")).getBody().getSubject();
Student student = studentRepository.findStudentByUsername(user);
Collection<GrantedAuthority> authorities = new ArrayList<>();
if (student != null) {
authorities.add(new SimpleGrantedAuthority("ROLE_STUDENT"));
}
return user != null ? new UsernamePasswordAuthenticationToken(user, null, authorities) : null;
}
return null;
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project OpenClinica by OpenClinica.
the class OpenClinicaLdapAuthoritiesPopulator method getGrantedAuthorities.
public Collection<GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {
Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>(1);
auths.add(new SimpleGrantedAuthority("ROLE_USER"));
return auths;
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project Activiti by Activiti.
the class BasicAuthenticationProvider method authenticate.
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
boolean authenticated = identityService.checkPassword(name, password);
if (authenticated) {
List<Group> groups = identityService.createGroupQuery().groupMember(name).list();
Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
for (Group group : groups) {
grantedAuthorities.add(new SimpleGrantedAuthority(group.getId()));
}
identityService.setAuthenticatedUserId(name);
return new UsernamePasswordAuthenticationToken(name, password, grantedAuthorities);
} else {
throw new BadCredentialsException("Authentication failed for this username and password");
}
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project ORCID-Source by ORCID.
the class StatsApiServiceBaseImplTest method init.
@Before
public void init() {
// create our mock data
List<StatisticValuesEntity> statsTimelineValues = new ArrayList<StatisticValuesEntity>();
List<StatisticValuesEntity> statsSummaryValues = new ArrayList<StatisticValuesEntity>();
StatisticValuesEntity a = new StatisticValuesEntity();
a.setId(1l);
a.setStatisticName(StatisticsEnum.KEY_LIVE_IDS.value());
a.setStatisticValue(100l);
StatisticKeyEntity akey = new StatisticKeyEntity();
akey.setGenerationDate(new Date(2000, 1, 1));
akey.setId(200L);
a.setKey(akey);
StatisticValuesEntity b = new StatisticValuesEntity();
b.setId(1l);
b.setStatisticName(StatisticsEnum.KEY_LIVE_IDS.value());
b.setStatisticValue(101l);
StatisticKeyEntity bkey = new StatisticKeyEntity();
bkey.setGenerationDate(new Date(1999, 1, 1));
bkey.setId(201L);
b.setKey(bkey);
StatisticValuesEntity c = new StatisticValuesEntity();
c.setId(1l);
c.setStatisticName(StatisticsEnum.KEY_NUMBER_OF_WORKS.value());
c.setStatisticValue(102l);
c.setKey(akey);
statsTimelineValues.add(a);
statsTimelineValues.add(b);
statsSummaryValues.add(a);
statsSummaryValues.add(c);
// mock the methods used
when(statisticsDao.getLatestKey()).thenReturn(akey);
when(statisticsDao.getStatistic(StatisticsEnum.KEY_LIVE_IDS.value())).thenReturn(statsTimelineValues);
when(statisticsDao.getStatistic(200l)).thenReturn(statsSummaryValues);
// mock the methods used
StatisticKeyEntity key200 = new StatisticKeyEntity();
key200.setId(200L);
key200.setGenerationDate(new Date(2000, 1, 1));
StatisticKeyEntity key201 = new StatisticKeyEntity();
key201.setId(201L);
key201.setGenerationDate(new Date(1999, 1, 1));
when(statisticsDao.getKey(200L)).thenReturn(key200);
when(statisticsDao.getKey(201L)).thenReturn(key201);
TargetProxyHelper.injectIntoProxy(statsManagerReadOnly, "statisticsDaoReadOnly", statisticsDao);
// setup security context
ArrayList<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
roles.add(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
Authentication auth = new AnonymousAuthenticationToken("anonymous", "anonymous", roles);
SecurityContextHolder.getContext().setAuthentication(auth);
}
Aggregations