return new User(username,password, AuthorityUtils.
commaSeparatedStringToAuthorityList("admin,normal"));,给我们的用户准备了2个权限,一个是admin,一个是normal。 建议:使用最简单最容易懂的antMatchers()。

- @Service
- public class UserServiceImpl implements UserDetailsService {
-
- @Autowired
- private PasswordEncoder passwordEncoder;
-
- @Override
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- if(StringUtils.isEmpty(username) || !(username.equals("admin"))){
- System.out.println("用户不存在!");
- throw new UsernameNotFoundException("用户记录不存在!");
- }
-
- // 到数据库查询用户记录
-
- String password = passwordEncoder.encode("123");
- // 给当前登录的用户设置:admin、normal权限
- // 给当前登录的用户设置:QQ、AA两个角色
- return new User(username,password, AuthorityUtils.
- commaSeparatedStringToAuthorityList("admin,normal,ROLE_QQ,ROLE_AA"));
- }
- }
- package com.yjxxt.springsecuritydemo.service;
- import org.springframework.security.core.Authentication;
- import javax.servlet.http.HttpServletRequest;
- public interface MyService {
- /* 描述:自定义方法
- * 作用:判断当前登录的用户是否具有权限
- * 参数:
- * 参数1:HttpServletRequest request。用于获取当前url。
- * 参数2:Authentication authentication:用于获取当前登录的用户
- * 返回:布尔值
- */
- boolean hasPermission(HttpServletRequest request, Authentication authentication);
- }
第三步:编写接口实现类:
- /**
- *
- * @since 1.0.0
- */
- @Component
- public class MyServiceImpl implements MyService {
- @Override
- public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
- Object obj = authentication.getPrincipal();
- if (obj instanceof UserDetails){
- UserDetails userDetails = (UserDetails) obj;
- Collection extends GrantedAuthority> authorities =userDetails.getAuthorities();
- return authorities.contains(newSimpleGrantedAuthority(request.getRequestURI()));
- }
- return false;
- }
- }
第四步:修改配置类:
- //url拦截
- http.authorizeRequests()
- //login.html不需要被认证
- // .antMatchers("/login.html").permitAll()
- .antMatchers("/login.html").access("permitAll")
- // .antMatchers("/main.html").hasRole("abc")
- .antMatchers("/main.html").access("hasRole('abc')")
- .anyRequest().access("@myServiceImpl.hasPermission(request,authentication)
- ")








工作流程1-me:


"status": 401
用非汉化的postman是成功的:
access_token:访问令牌;token_type:token的类型;expires_in:失效时间;scope:范围。
授权码只能用一次,如果获取不成功,那么只能重新获取新的授权码。
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
} /** * 密码模式 * @param endpoints * @throws Exception * 参数1:AuthorizationServerEndpointsConfigurer:授权服务器端点的配置 */ @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { // .authenticationManager(authenticationManager):授权管理器。 // .userDetailsService(userService):使用自定义的登录逻辑。 endpoints.authenticationManager(authenticationManager) .userDetailsService(userService); }
access:令牌。auth:授权。auth_to_access:授权到访问。client_id_to_access:授权到访问。



package com.yjxxt.controller;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/getCurrentUser")
public Object getCurrentUser(Authentication authentication) {
return authentication;
}
} @Override
public void configure(AuthorizationServerSecurityConfigurer security) {
// 获取密钥需要身份认证,使用单点登录时必须配置
security.tokenKeyAccess("isAuthenticated()");
} server.servlet.session.cookie.name=OAUTH2-CLIENT-SESSIONID01 security.oauth2.client.client-id=admin security.oauth2.client.client-secret=112233

@Override
public void configure(AuthorizationServerSecurityConfigurer security) {
// 获取密钥需要身份认证,使用单点登录时必须配置
security.tokenKeyAccess("isAuthenticated()");
}