认证步骤:
适用范围:此类型可用于有服务端的应用,是最贴近老版本的方式
response_type = code
client_id
redirect_uri
scope
state
code
state
grant_type = authorization_code
code
client_id
client_secret
redirect_uri
{
"access_token" : "",
"token_type" : "",
"expires_in" : 100,
"refresh_token" : ""
}
适用范围:不管有无服务端,此类型都可用 请求必须通过 http basic 进行验证(使用 client_id 和 client_secret)
grant_type = password
username
password
scope
{
"access_token": "91c37cb7-1868-45c3-9edd-475a236f0c28",
"token_type": "bearer",
"expires_in": 119,
"scope": "ALL",
"refresh_token": "3748bdd7-198c-4902-8322-0172954e0631"
}
适用范围:不管有无服务端,此类型都可用
grant_type = client_credentials
client_id
client_secret
scope
{
"access_token": "91c37cb7-1868-45c3-9edd-475a236f0c28",
"token_type": "bearer",
"expires_in": 119,
"scope": "ALL"
}
请求必须通过 http basic 进行验证(使用 client_id 和 client_secret)
grant_type = refresh_token
refresh_token
scope
{
"access_token": "91c37cb7-1868-45c3-9edd-475a236f0c28",
"token_type": "bearer",
"expires_in": 119,
"scope": "ALL",
"refresh_token": "3748bdd7-198c-4902-8322-0172954e0631"
}
1、在资源服务器的入口类或配置类加上注解
@EnableGlobalMethodSecurity(prePostEnabled = true)
2、参数说明
3、具体注解说明
@Secured注释是用来定义业务方法的安全性配置属性列表。您可以使用@Secured在方法上指定安全性要求[角色/权限等],只有对应角色/权限的用户才可以调用这些方法。如果有人试图调用一个方法,但是不拥有所需的角色/权限,那会将会拒绝访问将引发异常。 @Secured是从之前Spring版本中引入进来的。它有一个缺点(限制)就是不支持Spring EL表达式。 考虑下面的例子:
package com.yiibai.springsecurity.service; import org.springframework.security.access.annotation.Secured; public interface UserService { List<User> findAllUsers(); @Secured("ROLE_ADMIN") void updateUser(User user); @Secured({ "ROLE_DBA", "ROLE_ADMIN" }) void deleteUser(); }
Spring 的 @PreAuthorize/@PostAuthorize 注解是首选应用到方法级安全性的方式,并支持Spring表达式语言,也提供基于表达式的访问控制。 @PreAuthorize适合进入方法之前验证授权。 @PreAuthorize可以兼顾,角色/登录用户权限,参数传递给方法等等。 @PostAuthorize 虽然不经常使用,检查授权方法之后才被执行,所以它适合用在对返回的值作验证授权。Spring EL提供可在表达式语言来访问并从方法返回 returnObject 对象来反映实际的对象。 请参见常见内置表达式了解支持表达式的完整列表。让我们回到之前的例子,这一次使用 @PreAuthorize/@PostAuthorize 。
package com.yiibai.springsecurity.service; import org.springframework.security.access.prepost.PostAuthorize; import org.springframework.security.access.prepost.PreAuthorize; import com.yiibai.springsecurity.model.User; public interface UserService { List<User> findAllUsers(); @PostAuthorize ("returnObject.type == authentication.name") User findById(int id); @PreAuthorize("hasRole('ADMIN')") void updateUser(User user); @PreAuthorize("hasRole('ADMIN') AND hasRole('DBA')") void deleteUser(int id); }
表达式 | 描述 |
---|---|
hasRole([role]) | Returns true if the current principal has the specified role. By default if the supplied role does not start with 'ROLE_' it will be added. This can be customized by modifying the defaultRolePrefix on DefaultWebSecurityExpressionHandler. |
hasAnyRole([role1,role2]) | Returns true if the current principal has any of the supplied roles (given as a comma-separated list of strings). By default if the supplied role does not start with 'ROLE_' it will be added. This can be customized by modifying the defaultRolePrefix on DefaultWebSecurityExpressionHandler. |
hasAuthority([authority]) | Returns true if the current principal has the specified authority. |
hasAnyAuthority([authority1,authority2]) | Returns true if the current principal has any of the supplied roles (given as a comma-separated list of strings) |
principal | Allows direct access to the principal object representing the current user |
authentication | Allows direct access to the current Authentication object obtained from the SecurityContext |
permitAll | Always evaluates to true |
denyAll | Always evaluates to false |
isAnonymous() | Returns true if the current principal is an anonymous user |
isRememberMe() | Returns true if the current principal is a remember-me user |
isAuthenticated() | Returns true if the user is not anonymous |
isFullyAuthenticated() | Returns true if the user is not an anonymous or a remember-me user |
hasPermission(Object target, Object permission) | Returns true if the user has access to the provided target for the given permission. For example, hasPermission(domainObject, 'read') |
hasPermission(Object targetId, String targetType, Object permission) | Returns true if the user has access to the provided target for the given permission. For example, hasPermission(1, 'com.example.domain.Message', 'read') |
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )