0

多くの例を調べた後、Spring Security 構成を作成する方法を示す例を見つけることができませんが、ロールは注釈にリストされ、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>

春のセキュリティ.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 public class 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 アノテーションからロールをチェックするようにするには、何を記述すればよいですか?

4

1 に答える 1

0

移動してみてください

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

mvc-dispather-servlet.xmlあなたのコントローラーはによってではなくによってAdminピックアップされるため、への宣言。対応するFAQ エントリを参照してください。mvc-dispather-servlet.xmlspring-security.xml

于 2013-08-08T12:48:24.853 に答える