security java的配置_Spring Security:无法使用java config配置方法安...

我正在努力用java配置的spring安全性来配置方法安全性.我的配置没有任何问题,直到我在任何控制器中使用@Secured注释.

Spring Security Config:(java config)

@Configuration

@EnableWebSecurity

@EnableGlobalMethodSecurity(securedEnabled=true)

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired

private DataSource dataSource;

@Override

public void configure(WebSecurity web) throws Exception {

web

.ignoring()

.antMatchers("/webjars/**","/css/**", "/less/**","/img/**","/js/**");

}

@Autowired

public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception {

ShaPasswordEncoder shaPasswordEncoder = new ShaPasswordEncoder(256);

auth

.jdbcAuthentication()

.dataSource(dataSource)

.usersByUsernameQuery(getUserQuery())

.authoritiesByUsernameQuery(getAuthoritiesQuery())

.passwordEncoder(shaPasswordEncoder);

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http

.authorizeRequests()

.anyRequest().hasAuthority("BASIC_PERMISSION")

.and()

.formLogin()

.loginPage("/login")

.defaultSuccessUrl("/success-login", true)

.failureUrl("/error-login")

.loginProcessingUrl("/process-login")

.usernameParameter("security_username")

.passwordParameter("security_password")

.permitAll()

.and()

.logout()

.logoutSuccessUrl("/login")

.logoutUrl("/logout")

.permitAll()

.and()

.rememberMe()

.key("04E87501B3F04DB297ADB74FA8BD48CA")

.and()

.csrf()

.disable();

}

private String getUserQuery() {

return "SELECT username as username, password as password, active as enabled "

+ "FROM employee "

+ "WHERE username = ?";

}

private String getAuthoritiesQuery() {

return "SELECT DISTINCT employee.username as username, permission.name as authority "

+ "FROM employee, employee_role, role, role_permission, permission "

+ "WHERE employee.id = employee_role.employee_id "

+ "AND role.id = employee_role.role_id "

+ "AND role.id = role_permission.role_id "

+ "AND permission.id = role_permission.permission_id "

+ "AND employee.username = ? "

+ "AND employee.active = 1";

}

}

只要将@Secured(“EMPLOYEE_DELETE”)注释添加到任何控制器方法,我就会收到以下异常.

java.lang.IllegalArgumentException: Expecting to only find a single bean for type interface org.springframework.security.authentication.AuthenticationManager, but found []

所以我添加了一个AuthenticationManager bean:

@Bean

@Override

public AuthenticationManager authenticationManagerBean() throws Exception {

return super.authenticationManagerBean();

}

但是这会导致另一个错误:

java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configurers.provisioning.JdbcUserDetailsManagerConfigurer@34e81675 to already built object

似乎我必须与配置的jdbcAuthentication共享authenticationManagerBean,但我无法做到这一点.感谢您的帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值