写一个基于ssm校园兼职系统的登陆代码
时间: 2024-05-04 09:22:41 浏览: 106
首先,需要在数据库中建立一个用户表来存储用户信息,包括用户名、密码等等。
然后,在后端代码中,我们可以使用Spring Security框架来实现用户登录认证功能。具体步骤如下:
1. 添加Spring Security依赖
在pom.xml文件中添加以下依赖:
```xml
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.4.0</version>
</dependency>
```
2. 创建Spring Security配置类
创建一个继承自WebSecurityConfigurerAdapter的配置类,并重写configure()方法,配置用户认证规则和登录页面等信息。例如:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/resources/**", "/signup", "/about").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").failureUrl("/login?error").usernameParameter("username").passwordParameter("password")
.and()
.logout().logoutSuccessUrl("/login?logout")
.and()
.csrf();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
```
3. 实现UserDetailsService接口
创建一个实现UserDetailsService接口的类,用于从数据库中获取用户信息。例如:
```java
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserService userService;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userService.getUserByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found.");
}
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), getAuthorities(user));
}
private Collection<? extends GrantedAuthority> getAuthorities(User user) {
List<GrantedAuthority> authorities = new ArrayList<>();
for (Role role : user.getRoles()) {
authorities.add(new SimpleGrantedAuthority(role.getName()));
}
return authorities;
}
}
```
4. 创建登录页面
在前端代码中创建一个登录页面,包含用户名和密码的输入框和登录按钮。例如:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="/login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
5. 配置登录成功后跳转页面
在Spring Security配置类中添加以下代码,配置登录成功后跳转的页面:
```java
@Override
protected void configure(HttpSecurity http) throws Exception {
// ...
.formLogin().loginPage("/login").defaultSuccessUrl("/dashboard").failureUrl("/login?error").usernameParameter("username").passwordParameter("password")
// ...
}
```
以上就是一个基于SSM框架和Spring Security的登录认证功能的实现代码示例。
阅读全文