java 安全配置_java – Spring启动安全配置 – 必须指定authenticationManager

这是我的主要应用程序配置

@SpringBootApplication

public class Application {

public static void main(String[] args) {

new SpringApplicationBuilder(Application.class)

.banner((environment,aClass,printStream) ->

System.out.println(stringBanner()))

.run();

}

}

这是我的spring security应用程序配置.

@Configuration

@EnableGlobalMethodSecurity(prePostEnabled = true)

@EnableWebMvcSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired

private WebServiceAuthenticationEntryPoint unauthorizedHandler;

@Autowired

private TokenProcessingFilter authTokenProcessingFilter;

@Bean

@Override

public AuthenticationManager authenticationManagerBean() throws Exception {

return super.authenticationManagerBean();

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http

.csrf()

.disable()

.sessionManagement()

.sessionCreationPolicy(SessionCreationPolicy.STATELESS) // Restful hence stateless

.and()

.exceptionHandling()

.authenticationEntryPoint(unauthorizedHandler) // Notice the entry point

.and()

.addFilter(authTokenProcessingFilter) // Notice the filter

.authorizeRequests()

.antMatchers("/resources/**","/api/auth")

.permitAll()

.antMatchers("/greeting")

.hasRole("USER");

}

@Autowired

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

auth

.inMemoryAuthentication()

.withUser("user")

.password("password")

.roles("USER");

}

}

这是我的TokenProcessingFilter,它为我的自定义身份验证过滤器扩展了UsernamePasswordAuthenticationFilter

@Component

public class TokenProcessingFilter extends UsernamePasswordAuthenticationFilter {

@Override

public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException {

HttpServletRequest httpRequest = this.getAsHttpRequest(request);

String authToken = this.extractAuthTokenFromRequest(httpRequest);

String userName = TokenUtils.getUserNameFromToken(authToken);

if (userName != null) {/*

UserDetails userDetails = userDetailsService.loadUserByUsername(userName);*/

UserDetails userDetails = fakeUserDetails();

if (TokenUtils.validateToken(authToken,userDetails)) {

UsernamePasswordAuthenticationToken authentication =

new UsernamePasswordAuthenticationToken(userDetails.getUsername(),userDetails.getPassword(),userDetails.getAuthorities());

authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));

SecurityContextHolder.getContext().setAuthentication(authentication);

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

}

}

chain.doFilter(request,response);

}

private HttpServletRequest getAsHttpRequest(ServletRequest request){

if (!(request instanceof HttpServletRequest)) {

throw new RuntimeException("Expecting an HTTP request");

}

return (HttpServletRequest) request;

}

private String extractAuthTokenFromRequest(HttpServletRequest httpRequest) {

/* Get token from header */

String authToken = httpRequest.getHeader("x-auth-token");

/* If token not found get it from request parameter */

if (authToken == null) {

authToken = httpRequest.getParameter("token");

}

return authToken;

}

private UserDetails fakeUserDetails(){

UsernamePasswordAuthenticationToken authenticationToken = new

UsernamePasswordAuthenticationToken("user","password");

List auth= new ArrayList<>();

auth.add(new SimpleGrantedAuthority("USER"));

return new User("user","password",auth);

}

}

但是在运行应用程序时,我遇到此异常消息.我错过了什么?

An exception occured while running. null: InvocationTargetException:

Unable to start embedded container; nested exception is

org.springframework.boot.context.embedded.EmbeddedServletContainerException:

Unable to start embedded Tomcat: Error creating bean with name

‘tokenProcessingFilter’ defined in file

[C:\Users\kyel\projects\app\target\classes\org\app\testapp\security\TokenProcessingFilter.class]:

Invocation of init method Failed; nested exception is

java.lang.IllegalArgumentException: authenticationManager must be

specified

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值