java - 基于用户权限的休眠会话
问题描述
我是 Hibernate 的新手,并完成了一些小项目。
目前我想在 Hibernate 中创建基于用户权限的会话,以便用户可以访问他/她有权访问的数据。
我的数据库是 pgadmin 9.1。
Hibernate 版本是 4.3.0 Final。
我将它与 Spring Framework MVC 一起使用。
解决方案
您可以使用 Spring 安全性来管理会话。
您可以在三个级别上管理访问权限:
- 爪哇
- 视图(JSP、HTML 等)
- XML (安全上下文.xml)
这是 security-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.1.xsd">
<!-- We will be defining all security related configurations in this file -->
<!-- <http auto-config="true"></http> -->
<http pattern="/resources/**" security="none" />
<http pattern="/passWordRec" security="none" />
<http pattern="/PassRec" security="none" />
<http pattern="/passWordRecSuccFail" security="none" />
<http pattern="/login" security="none" />
<!-- <http pattern="/downloadUsersPDF" security="none" /> -->
<http use-expressions="true" disable-url-rewriting="true" auto-config="false">
<intercept-url pattern="/downloadUsersPDF" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/downloadInformationUserPdf" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/downloadWatchPdf" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/downloadPatientWatchPdf" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/downloadSupervisorPdf" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/**" access="isAuthenticated()" /><!-- this means all URL in this app will be checked if user is authenticated -->
<form-login login-page="/login" default-target-url="/index-2"
authentication-failure-url="/login?error" always-use-default-target="true"/> <!-- We will just use the built-in form login page in Spring -->
<logout logout-url="/logout" logout-success-url="/login?logout" />
<!-- <logout delete-cookies="JSESSIONID" /> the logout url we will use in JSP -->
<remember-me key="uniqueAndSecret"/>
</http>
<authentication-manager>
<authentication-provider user-service-ref="customUserDetailsService" >
<password-encoder hash="sha-256">
<salt-source user-property="username"/>
</password-encoder>
</authentication-provider>
</authentication-manager>
</beans:beans>
有关更多详细信息,您可以查看此项目。
推荐阅读
- django - 检查用户名是否存在于数据库中
- javascript - BABEL 注意:代码生成器对 D:\xxxxx\middleware\dist\_nuxt\6491bb123b397e949a0b.js 的样式进行了优化,因为它超过了 500KB 的最大值
- r - 如何将列拆分为多个列并在 R 中找到频率?
- performance - 并行 for 循环导致 OpenMP 性能下降
- android - 如何在 Android 9.0 上获取系统应用权限
- android-studio - 使用 Provider.of 时,必须向 Text 小部件提供非空字符串
(语境) - python-3.x - Pytest 模拟 for 循环使用的内容管理器
- spring - 如何转换正常的 CrudRepository
到 ReactiveCrudRepository - java - 当前时间/日期事件监听器
- templates - InteliJ 实时模板