繁体   English   中英

具有Hibernate和Annotations和基本HTTP身份验证的Spring安全性

[英]Spring security with Hibernate and Annotations and basic HTTP authentication

在研究了许多示例之后,我找不到任何示例说明如何创建Spring Security配置,而Roles列在注释中,而Hibernate用于身份验证。

我的文件:

mvc-dispather-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:security="http://www.springframework.org/schema/security"
   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/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">
    <import resource="classpath:hibernate-beans.xml" />

    <mvc:annotation-driven/>
    <context:annotation-config/>
    <context:component-scan base-package="com.salespredict"/>

</beans>

spring-security.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:security="http://www.springframework.org/schema/security"
         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.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http>
     <http-basic/>
</http>

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="authenticationService" />
</authentication-manager>

<global-method-security secured-annotations="enabled" />

</beans:beans>

服务:

@Service公共类AuthenticationService实现UserDetailsS​​ervice {

@Autowired
private IUserRepository userRepository;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = userRepository.findOne(username);
    Set<Role> roles = user.getRoles();
    Set<GrantedAuthority> authorities = new HashSet<>();
    for(Role role:roles) {
        authorities.add(new SimpleGrantedAuthority(role.getRole().name()));
    }
    return new org.springframework.security.core.userdetails.User(
            user.getUsername(),
            user.getPassword(),
            authorities);
}

}

控制器:

@Controller
@Secured({RoleNames.ADMIN, RoleNames.SALES_PREDICT_ADMIN})
@RequestMapping("/admin")
public class Admin extends WebServiceBase {


    @RequestMapping(value = "/users", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json")
    public
    @ResponseBody
    ResponseEntity registerNewUsers(InputStream data) throws Exception {
        // deserialize from JSON
        Users users = _mapper.readValue(data, Users.class);
        PutUsers msg = new PutUsers(users.getUsers());
        postMessage(msg, DefaultResponse.class);
        return ok();
     }
    ...
    }

如果我将<http>更改为

 <http use-expressions="true">
    <intercept-url pattern="/**" access="isAuthenticated()" />
    <http-basic />
</http>

然后,将调用我的身份验证服务,但它仅检查用户是否提供密码,而不检查角色。 如果删除它,则根本不会调用身份验证服务。

我应该在<intercept-url pattern="/**" access= ... >写什么,以使其通过@Secured注释检查角色?

尝试移动你的

<global-method-security secured-annotations="enabled" />

声明为mvc-dispather-servlet.xml因为您的Admin控制器由mvc-dispather-servlet.xml而不是spring-security.xml拾取。 请参阅相应的常见问题解答条目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM