帮我用springsecurity实现app登录以及获取用户当前登录信息
时间: 2024-02-15 22:05:38 浏览: 140
好的,以下是使用Spring Security实现APP登录和获取用户当前登录信息的步骤:
1. 添加Spring Security依赖
在后端的pom.xml中添加以下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
2. 配置Spring Security
在后端的application.properties中添加以下配置:
```
# 禁用CSRF保护,以便移动端使用RESTful API
spring.security.csrf.enabled=false
# 配置用户信息,这里使用了内存存储
spring.security.user.name=admin
spring.security.user.password=password
spring.security.user.roles=USER
```
3. 创建RESTful API接口
在后端创建一个登录接口,用于接收移动端传递的用户名和密码,并返回认证结果。以下是一个简单的示例:
```
@RestController
public class LoginController {
@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody LoginRequest loginRequest) {
// 在这里进行用户认证,返回认证结果
return ResponseEntity.ok(new LoginResponse("认证成功"));
}
public static class LoginRequest {
private String username;
private String password;
// 省略getter和setter
}
public static class LoginResponse {
private String message;
// 省略getter和setter
}
}
```
4. 创建一个RESTful API接口,用于获取当前登录用户信息
```
@RestController
public class UserController {
@GetMapping("/user")
public ResponseEntity<User> getCurrentUser(Authentication authentication) {
User user = (User) authentication.getPrincipal();
return ResponseEntity.ok(user);
}
public static class User {
private String username;
private List<String> roles;
// 省略getter和setter
}
}
```
5. 在移动端实现登录和获取用户信息功能
在移动端实现登录和获取用户信息功能,具体实现方式根据移动端技术栈不同而有所区别。以下是一个简单的示例:
```
public class LoginActivity extends AppCompatActivity {
private EditText usernameEditText;
private EditText passwordEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
Button loginButton = findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
LoginRequest loginRequest = new LoginRequest(username, password);
loginUser(loginRequest);
}
});
}
private void loginUser(LoginRequest loginRequest) {
String url = "http://localhost:8080/login";
String requestBody = new Gson().toJson(loginRequest);
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, requestBody);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
OkHttpClient client = new OkHttpClient();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseBody = response.body().string();
LoginResponse loginResponse = new Gson().fromJson(responseBody, LoginResponse.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, loginResponse.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
}
public static class LoginRequest {
private String username;
private String password;
// 省略getter和setter
}
public static class LoginResponse {
private String message;
// 省略getter和setter
}
}
```
以上是一个简单的示例,实际情况中需要根据具体需求进行修改和完善。
阅读全文