Spring Security related issues

by kpkmd54461 on 2012-02-28 18:35:53

Here is the translation of your question and the provided solution into English:

---

**Question:**

In a project using Spring Security for login, I want to handle the login process myself. The problem is, after a successful login, how do I create an authenticated `Authentication` object and place it in the `SecurityContextHolder`?

---

**Supplementary Question:**

enet_java wrote: Create an `Authentication` object, then use `SecurityContextHolder.getContext().setAuthentication(authenticated)` to save the authenticated `Authentication` object for future use.

Could you provide some code or help me check the following code snippet?

```java

org.springframework.security.core.userdetails.User userdetail =

new org.springframework.security.core.userdetails.User(

userName,

admin.getPassword(),

true,

accountNonExpired,

credentialsNonExpired,

unableToMapPath, // This seems incorrect

accountNonLocked,

grantedAuths);

UsernamePasswordAuthenticationToken authen =

new UsernamePasswordAuthenticationToken(

userdetail,

java.util.MissingFormatArgumentException, // This seems incorrect

admin.getPassword());

WebAuthenticationDetails webdetails = new WebAuthenticationDetails(request);

authen.setDetails(webdetails);

SecurityContextHolder.getContext().setAuthentication(aa); // 'aa' should be 'authen'

```

The issue has been resolved as follows:

```java

GrantedAuthority[] grantedAuths = obtainGrantedAuthorities(admin);

boolean accountNonExpired = true;

boolean credentialsNonExpired = true;

boolean accountNonLocked = true;

org.springframework.security.core.userdetails.User userdetail =

new org.springframework.security.core.userdetails.User(

userName,

admin.getPassword(),

true,

accountNonExpired,

credentialsNonExpired,

accountNonLocked,

grantedAuths);

UsernamePasswordAuthenticationToken authen =

new UsernamePasswordAuthenticationToken(userdetail, admin.getPassword());

WebAuthenticationDetails webdetails = new WebAuthenticationDetails(request);

authen.setDetails(webdetails);

SecurityContextHolder.getContext().setAuthentication(authen);

```

---

**Solution:**

- Create an `Authentication` object, then use `SecurityContextHolder.getContext().setAuthentication(authenticated)` to save the authenticated `Authentication` object for future use.

- For more details, you can refer to this resource on Spring Security issues:

[Spring Security Related Issues](http://www.myexception.cn/j2ee/17182.html)

---

**Related Topics:**

- Does the Android platform support CHM format files? If so, what needs to be installed?

- Regarding SSH integration, there are always errors being reported.

- How should ADF controllers be handled?

---

**Code Corrections:**

1. Replace `unableToMapPath` with `true` (or the correct value for `accountNonExpired`).

2. Replace `java.util.MissingFormatArgumentException` with `admin.getPassword()` (or the correct password value).

3. Ensure that `SecurityContextHolder.getContext().setAuthentication()` uses the correct variable (`authen` instead of `aa`).

---

This should resolve the issue and ensure proper authentication handling in your Spring Security implementation.