Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring安全服务配置_Java_Spring Mvc_Authentication_Spring Security - Fatal编程技术网

Java Spring安全服务配置

Java Spring安全服务配置,java,spring-mvc,authentication,spring-security,Java,Spring Mvc,Authentication,Spring Security,我正在尝试使用不同的框架构建一个JavaEE应用程序原型。除了安全层,其他一切都正常工作。我选择使用Spring配置的Spring安全性 代码如下所示: 春季安全配置 @Configuration @EnableWebMvcSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private MyUserDetailsService userDetailsSer

我正在尝试使用不同的框架构建一个JavaEE应用程序原型。除了安全层,其他一切都正常工作。我选择使用Spring配置的Spring安全性

代码如下所示:

春季安全配置

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    @Autowired
    private MyUserDetailsService userDetailsService;

    @Override
    protected UserDetailsService userDetailsService () {
        return this.userDetailsService;
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
        .ignoring()
        .antMatchers("/resources/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .formLogin()
        .loginPage("/login")
        .loginProcessingUrl("/login/authenticate")
        .failureUrl("/login?error=bad_credentials")
        .and()
        .logout()
        .logoutUrl("/signout")
        .deleteCookies("JSESSIONID")
        .and()
        .authorizeRequests()
        .antMatchers("/admin/**").hasRole("ADMIN")
        .antMatchers("/**").permitAll()
        .and()
        .csrf();
    }
}
用户详细信息服务

@Service("myUserDetailsService")
public class MyUserDetailsService implements UserDetailsService
{
    public static final Logger log = Logger.getLogger(MyUserDetailsService.class);

    public MyUserDetailsService() {
    }

    @Autowired
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        final User user = getSystemUser(userName);
        final List<GrantedAuthority> authorities = getUserAuthorities(user);
        return buildUserForAuthentication(user, authorities);
    }

    private User buildUserForAuthentication(User user, List<GrantedAuthority> authorities) {
        //...
    }

    private User getSystemUser(String alias) {
        //...
    }

    private List<GrantedAuthority> getUserAuthorities(User user) {
        //...
        return null;
    }
}
@Service(“myUserDetailsService”)
公共类MyUserDetailsService实现UserDetailsService
{
公共静态最终记录器log=Logger.getLogger(MyUserDetailsService.class);
公共MyUserDetailsService(){
}
@自动连线
私有UserDao UserDao;
@凌驾
public UserDetails loadUserByUsername(字符串用户名)引发UsernameNotFoundException{
最终用户=getSystemUser(用户名);
最终列表权限=GetUserAuthories(用户);
返回buildUserForAuthentication(用户、权限);
}
私人用户buildUserForAuthentication(用户、列表权限){
//...
}
私有用户getSystemUser(字符串别名){
//...
}
私有列表GetUserAuthories(用户){
//...
返回null;
}
}
我希望这段代码能够做到的是,当用户&pass参数到达
/login/authenticate
时,底层的spring代码调用我的用户服务,但这永远不会发生

我错过了什么


我使用的是spring security 3.2.3.RELEASE。

您应该在SecurityConfig类中注册自定义身份验证,该类扩展了WebSecurity配置适配器:

@Autowired
private MyUserDetailsService userDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) 
      throws Exception {
    auth.userDetailsService(this.userDetailsService);
}
对于3.2.3,配置应为

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(this.userDetailsService);
}

似乎方法(registerAuthentication(AuthenticationManagerBuilder auth))不是WebSecurity配置适配器的一部分。您从哪个类扩展?我错过了您在用户手册中提到的版本,我认为对于3.2.3,配置应该是@Autowired public void configureGlobal(authenticationmanager builder auth){,我会在问题中编辑你完全正确!与此同时,我在谷歌上搜索了这个问题,并在该版本的Spring changelog中发现该方法已被重命名。我重写了我的代码,现在它工作了!非常感谢你的帮助!问候你。