我接下来的课程:class User{private int id;private String email;private String password;}class Admin extends User{// the same fields as in User class}class REDAdmin extends User{private String compan...
我接下来的课程:
class User{
private int id;
private String email;
private String password;
}
class Admin extends User{
// the same fields as in User class
}
class REDAdmin extends User{
private String company;
private String description;
}
class Customers extends User{
private String FirstName;
private String LastName;
....
}
在我的数据库中,我不需要任何Role表
安全的context.xml
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/REDadmin**" access="hasRole('ROLE_REDADMIN')" />
<intercept-url pattern="/user**" access="hasRole('ROLE_USER')" />
<!-- access denied page -->
<access-denied-handler error-page="/403" />
<form-login
login-page="/login"
default-target-url="/welcome"
authentication-failure-url="/login?error"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf/>
</http>
解决方法:
如果您不需要使用关系管理角色,则可以在实体类中返回一组固定的角色.用户/帐户应实施UserDetails合同 – 例如:
class User implements UserDetails {
private final Set<GrantedAuthority> authorities = new HashSet<>();
public User() {
authorities.add(new SimpleGrantedAuthority("USER"));
// ... add further roles if required
}
public Collection<GrantedAuthority> getAuthorities() {
return authorities;
}
...
}
见:GrantedAuthority,SimpleGrantedAuthority
沃梦达教程
本文标题为:java – 如何在不使用数据库中的任何角色表的情况下编写spring安全性?
基础教程推荐
猜你喜欢
- 详解RabbitMq如何做到消息的可靠性投递 2023-05-13
- RocketMQ Push 消费模型示例详解 2023-05-24
- Java冒泡排序及优化介绍 2023-08-11
- 详解如何在Java中加密和解密zip文件 2023-06-01
- mybatis-plus实现自定义SQL、多表查询与多表分页查询语句实例 2023-05-19
- 图解Java经典算法冒泡排序的原理与实现 2023-05-14
- Java比较两个对象大小的三种方法详解 2023-02-28
- java – 从Solr DataImportHandler中的Oracle日期获取正确的时间 2023-11-03
- Java Redisson多策略注解限流 2023-05-25
- 基于Zookeeper实现分布式锁详解 2023-08-10