下面列出了怎么用org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder的API类实例代码及写法,或者点击链接到github查看源代码。
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
// 设置UserDetailsService
.userDetailsService(userDetailsService())
// 使用BCrypt进行密码的hash
.passwordEncoder(passwordEncoder());
auth.eraseCredentials(false);
}
/**
* 模拟数据库查询,判断尝试登陆用户是否满足认证条件
*
* @param auth AuthenticationManagerBuilder
* @throws Exception 异常
*/
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password(passwordEncoder().encode("123456")).roles("USER")
.and()
.withUser("user2").password(passwordEncoder().encode("qweasd")).roles("USER")
.and()
.withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("user1")
.password(passwordEncoder().encode("user"))
.roles("USER")
.and()
.withUser("admin")
.password(passwordEncoder().encode("admin"))
.roles("ADMIN");
}
/**
* Registers the KeycloakAuthenticationProvider with the authentication manager.
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
// adding proper authority mapper for prefixing role with "ROLE_"
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
auth.authenticationProvider(keycloakAuthenticationProvider);
}
/**
* Setup 2 users with different roles
*/
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth.jdbcAuthentication().dataSource(dataSource).withUser("dave")
.password("secret").roles("USER");
auth.jdbcAuthentication().dataSource(dataSource).withUser("anil")
.password("password").roles("ADMIN");
// @formatter:on
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
logger.info(String.format("%n%nUsing generated security password for state API: %s%n", properties().getPassword()));
// configure default backend user
auth
.inMemoryAuthentication()
.withUser(properties().getUsername()).password(bCrypt.encode(properties().getPassword())).authorities("ROLE_STATE");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user1")
.password(passwordEncoder().encode("user1Pass"))
.authorities("ROLE_USER");
}
@SuppressWarnings("javadoc")
@Inject
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("waiter").password(this.passwordEncoder.encode("waiter")).roles("Waiter")
.and().withUser("cook").password(this.passwordEncoder.encode("cook")).roles("Cook").and().withUser("barkeeper")
.password(this.passwordEncoder.encode("barkeeper")).roles("Barkeeper").and().withUser("chief")
.password(this.passwordEncoder.encode("chief")).roles("Chief");
}
@Override
protected void configure(AuthenticationManagerBuilder builder) throws Exception{
builder
.authenticationProvider(socialAuthenticationProvider())
.authenticationProvider(rememberMeAuthenticationProvider())
.userDetailsService(userAccountService);
}
@Override
public void configure(AuthenticationManagerBuilder auth) {
CrustAuthenticationProvider authenticationProvider = new CrustAuthenticationProvider(props, passwordEncoder);
configureProvider(authenticationProvider);
// 添加自定义身份验证组件
auth.authenticationProvider(authenticationProvider);
}
public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder, UserDetailsService userDetailsService,TokenProvider tokenProvider,CorsFilter corsFilter, SecurityProblemSupport problemSupport) {
this.authenticationManagerBuilder = authenticationManagerBuilder;
this.userDetailsService = userDetailsService;
this.tokenProvider = tokenProvider;
this.corsFilter = corsFilter;
this.problemSupport = problemSupport;
}
public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder, UserDetailsService userDetailsService, TokenProvider tokenProvider, CorsFilter corsFilter, SecurityProblemSupport problemSupport) {
this.authenticationManagerBuilder = authenticationManagerBuilder;
this.userDetailsService = userDetailsService;
this.tokenProvider = tokenProvider;
this.corsFilter = corsFilter;
this.problemSupport = problemSupport;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder pwdEncoder = new BCryptPasswordEncoder();
auth.inMemoryAuthentication()
.passwordEncoder(pwdEncoder)
.withUser("admin")
.password(pwdEncoder.encode("admin12345678"))
.roles("ADMIN", "MEMBER")
.and()
.withUser("caterpillar")
.password(pwdEncoder.encode("12345678"))
.roles("MEMBER");
}
@Override
protected void configure(AuthenticationManagerBuilder authMgrBldr) throws Exception {
authMgrBldr.inMemoryAuthentication()
.passwordEncoder(org.springframework.security.crypto.password.NoOpPasswordEncoder.getInstance())
.withUser(DEFAULT_USER_ID).password(DEFAULT_USER_SECRET).authorities(DEFAULT_USER_ROLE).and()
.withUser(DEFAULT_ADMIN_ID).password(DEFAULT_ADMIN_SECRET)
.authorities(DEFAULT_USER_ROLE, DEFAULT_ADMIN_ROLE);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// Store in memory
// auth
// .inMemoryAuthentication()
// .withUser("john.carnell").password("password1").roles("USER") // Define the first user: john.carnell with the password "password1" and the role "USER"
// .and()
// .withUser("william.woodward").password("password2").roles("USER", "ADMIN"); // Define the second user: william.woodward with the password "password2" and the role "ADMIN"
// Store in databse
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder);
}
@Autowired
public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
// 设置UserDetailsService
.userDetailsService(this.userDetailsService)
// 使用BCrypt进行密码的hash
.passwordEncoder(passwordEncoder());
//remember me
authenticationManagerBuilder.eraseCredentials(false);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())
.dataSource(dataSource())
.usersByUsernameQuery("select name, password, enabled from t_account where name=?")
.authoritiesByUsernameQuery("select name, role from t_account_role where name=?");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("USER");
}
/**
* 注入自定义的userDetailsService实现,获取用户信息,设置密码加密方式
*
* @param authenticationManagerBuilder
* @throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
// 设置手机验证码登陆的AuthenticationProvider
authenticationManagerBuilder.authenticationProvider(mobileAuthenticationProvider());
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select email,password,enabled "
+ "from bael_users "
+ "where email = ?")
.authoritiesByUsernameQuery("select email,authority "
+ "from authorities "
+ "where email = ?");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService())
.passwordEncoder(passwordEncoder());
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userRepository);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER")
.and().withUser("admin").password("password").roles("USER", "ADMIN");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authProvider());
auth.eraseCredentials(false);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 指定 provider
auth.authenticationProvider(customAuthenticationProvider);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService())
.passwordEncoder(passwordEncoder());
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(Service()).passwordEncoder(new BCryptPasswordEncoder() {
});
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(Service()).passwordEncoder(new BCryptPasswordEncoder() {
});
}
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
auth.authenticationProvider(authProvider());
}