Spring Security HTTP基本认证示例

配置HTTP基本身份验证后,Web浏览器将显示一个用于用户身份验证的登录对话框。 本教程向您展示如何在Spring Security中配置HTTP基本认证。

<http>
	<intercept-url pattern="/welcome*" access="ROLE_USER" />
	<http-basic />
  </http>

最后一个基于Spring Security表单的登录示例将被重用,但是切换身份验证以支持HTTP基本。

1.Spring安全

要启用HTTP基本功能,只需将“ form-login ”更改为“ http-basic ”标记。

<beans:beans xmlns="http://www.springframework.org/schema/security"
	xmlns:beans="http://www.springframework.org/schema/beans" 
	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/security
	http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

	<!-- HTTP basic authentication in Spring Security -->
	<http>
		<intercept-url pattern="/welcome*" access="ROLE_USER" />
		<http-basic />
	</http>

	<authentication-manager>
	   <authentication-provider>
	       <user-service>
		   <user name="mkyong" password="123456" authorities="ROLE_USER" />
	       </user-service>
	   </authentication-provider>
	</authentication-manager>

</beans:beans>

完成,仅此而已。

2.演示

访问安全的URL时,浏览器将自动显示一个登录对话框。

网址:http:// localhost:8080 / SpringMVC / welcome

http basic example

下载源代码

下载它-Spring-Security-HTTP-Basic-Authentication-Example.zip (9 KB)

参考文献

  1. Spring Security你好世界示例
  2. 基于Spring Security表单的登录示例

翻译自: https://mkyong.com/spring-security/spring-security-http-basic-authentication-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本Spring Security身份验证示例: 1. 定义一个UserDetailsService实现类,用于从用户存储中获取用户详细信息并返回UserDetails对象。示例代码如下: ```java @Service public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found with username: " + username); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), getAuthorities(user)); } private Set<GrantedAuthority> getAuthorities(User user) { Set<GrantedAuthority> authorities = new HashSet<>(); for (Role role : user.getRoles()) { authorities.add(new SimpleGrantedAuthority(role.getName())); } return authorities; } } ``` 2. 配置Spring Security,包括安全策略和授权规则。示例代码如下: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasRole("USER") .anyRequest().authenticated() .and() .formLogin() .and() .logout() .logoutUrl("/logout") .invalidateHttpSession(true) .deleteCookies("JSESSIONID") .logoutSuccessUrl("/"); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 3. 在控制器中定义安全受限的请求处理程序。示例代码如下: ```java @Controller public class HomeController { @GetMapping("/") public String home() { return "home"; } @GetMapping("/admin") public String admin() { return "admin"; } @GetMapping("/user") public String user() { return "user"; } } ``` 在上述示例中,UserDetailsService实现类用于从用户存储中获取用户详细信息,例如用户名、密码和角色。SecurityConfig类配置Spring Security,包括安全策略和授权规则。HomeController控制器定义安全受限的请求处理程序,例如主页、管理员页面和用户页面。这些请求只有在用户进行身份验证并被授予相应的角色时才可以访问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值