Skip to content

Commit

Permalink
Remove the ROLE_ prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengjie committed Mar 1, 2019
1 parent b5b3056 commit 7fbefa9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.core.GrantedAuthorityDefaults;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand Down Expand Up @@ -50,6 +50,12 @@ public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
.passwordEncoder(passwordEncoderBean());
}

@Bean
GrantedAuthorityDefaults grantedAuthorityDefaults() {
// Remove the ROLE_ prefix
return new GrantedAuthorityDefaults("");
}

@Bean
public PasswordEncoder passwordEncoderBean() {
return new BCryptPasswordEncoder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author jie
Expand Down Expand Up @@ -71,15 +69,7 @@ public boolean isEnabled() {
return enabled;
}

/**
* 在我们保存权限的时候加上了前缀ROLE_,因此在这里需要处理下数据
* @return
*/
public Collection getRoles() {
Set<String> roles = new LinkedHashSet<>();
for (GrantedAuthority authority : authorities) {
roles.add(authority.getAuthority().substring(5));
}
return roles;
return authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static List<GrantedAuthority> mapToGrantedAuthorities(Set<Role> roles,Pe
}

return permissions.stream()
.map(permission -> new SimpleGrantedAuthority("ROLE_"+permission.getName()))
.map(permission -> new SimpleGrantedAuthority(permission.getName()))
.collect(Collectors.toList());
}
}

0 comments on commit 7fbefa9

Please sign in to comment.