当前位置: 首页 > 知识库问答 >
问题:

带有hibernate:hasRole()的Spring Security性在配置中不起作用

张鹏鹍
2023-03-14

我正在尝试为我的Spring+Hibernate项目实现Spring Security性。

但是我在intercept-url标记中编写的hasRole('super_admin')不起作用。

请找到我在下面所做的配置。

<http auto-config="true" use-expressions="true">
        <intercept-url pattern="/admin**" access="hasRole('SUPER_ADMIN')" />
        <!-- 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"
            login-processing-url="/loginCheck" password-parameter="password" />
        <logout logout-success-url="/login?logout" />
        <!-- enable csrf protection -->
        <csrf />
    </http>

    <authentication-manager>
        <authentication-provider user-service-ref="myUserDetailsServices">
            <password-encoder hash="bcrypt" />
        </authentication-provider>
    </authentication-manager>
public class MyUserDetailsServices implements UserDetailsService {

    private UserDao userDao;

    @Override
    @Transactional
    public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {

        User user = userDao.findByEmail(username);
        if (user == null) {
            throw new UsernameNotFoundException("User " + username + " not found");
        }
        List<GrantedAuthority> authorities = buildUserAuthority(user.getRoles());
        return buildUserForAuthentication(user, authorities);

    }

    private org.springframework.security.core.userdetails.User buildUserForAuthentication(User user, List<GrantedAuthority> authorities) {
        return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), true, true, true, true, authorities);
    }

    private List<GrantedAuthority> buildUserAuthority(Set<Role> userRoles) {

        Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();

        for (Role userRole : userRoles) {
            setAuths.add(new SimpleGrantedAuthority(userRole.getRoleName()));
        }

        List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);

        return Result;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}

但hasRole('super_admin')仍然不工作。我无法访问http://127.0.0.1:8080/myproj/admin页面。用户正在进行身份验证并登录。但是上面的url被重定向到/403(访问被拒绝)。

我是不是漏掉了什么?请帮帮我!

共有1个答案

竺承望
2023-03-14

HasRole运行良好。代码中不起作用的是SpringSecurity.xml上的通配符。更改此

 类似资料:
  • 问题内容: 更新的问题 我的虚拟主机 问题答案: 这是问题的最可能原因。您已经在超级用户的主文件夹中创建了virtualenv。但是该文件夹不太可能被Apache访问。默认情况下,其他任何用户都无法访问用户的主文件夹。 Web服务器和WSGI过程将运行作为一个非特权用户通常命名为,,或者类似的东西。虽然您可以通过更改/ root /上的权限来解决此问题,但这不是很大。如果是普通用户,则危险性会降低

  • 我卡住了!如果跳过测试并部署到tomcat自动布线,则配置属性文件将正常工作。在我的测试中,它失败了!我不确定我错过了什么。 GitService-自带属性在Tomcat上工作! GitServiceTest由于NPE,该类在初始化时失败.属性为空。 粘贴后我确实意识到@EnableConfigurationProperties在application.java和GitService.java类上

  • 问题内容: 我正在使用以下代码,但在IE中失败。消息是: 无法获取属性’add’的值:对象为null或未定义” 我认为这只是一个IE支持问题。您将如何使以下代码在IE中工作? 有任何想法吗? 问题答案: IE9及更低版本不支持该属性。IE10 +支持它。 使用代替。注意:不要忽略空格:类名应添加在以空格分隔的列表中。

  • 问题内容: 我正在用CSS过渡在鼠标悬停时从右到左设置一个容器动画。在InternetExplorer以外的所有浏览器中,此功能均正常运行。原因是我在CSSleft属性中使用了(并且需要使用)calc()。 CSS看起来像这样: 我正在使用jQuery在mouseover上添加.translate-less类: 现在,我想在Internet Explorer中顺利过渡。为此,我什至放弃了这些特定浏

  • 我有使用碎片的活动。但如果我想使用OnClickListener,我的应用程序就会崩溃。如果我不使用OnClickListener,那么一切都可以。我该如何解决这个问题呢?这是我的代码。 d/gralloc_goldfish(796):检测到没有GPU仿真的仿真程序。d/AndroidRuntime(923):Threadid=1:线程退出但未捕获异常(组=0x41465700)致命异常:Main

  • 下面是创建基于自定义受众的远程配置条件的步骤- 首先创建了一个名为OEM的用户属性 我用创建了一个动态链接,作为 https://d83j2.app.goo.gl/?link=http://myapp.in&apn=com.myapp.app&utm_source=google-micromax&utm_medium=micromax_device&utm_campaign=promo_googl