461784 (3) [Avatar] Offline
#1
Hello!

First of all thanks for the great book! A real deal breaker! I've managed to setup the Authorization Service from Chapter 7.2! I can authorize my users and I get the token. However when I try to invoke my token details by using this POST command:
http://localhost:8999/auth/user


I get a 500 internal server error telling me that: "java.lang.NullPointerException" which I find is related to this piece of code.

@RequestMapping(value = { "/user" }, produces = "application/json")
    public Map<String, Object> user(OAuth2Authentication user) {
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("user", user.getUserAuthentication().getPrincipal());
        userInfo.put("authorities", AuthorityUtils.authorityListToSet(user.getUserAuthentication().getAuthorities()));
        return userInfo;
    }


More specifically I see that OAuth2Authentication is null. It seems to me that some configuration is missing in either the WebSecurityConfigurer or OAuth2Config classes.

I've checked everything and I see no errors on my side, because the code is copy pasted from your repo ex Master branch. Maybe there is something missing? In the book it states that this code should work.

Thanks in advance.
461784 (3) [Avatar] Offline
#2
Ok I understood what went wrong, this was due to my setup in WebSecurityConfigurer class. I had to remove these two methods which were csrf related:

	private CsrfTokenRepository csrfTokenRepository() {
		HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
		repository.setSessionAttributeName("_csrf");
		return repository;
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().csrfTokenRepository(csrfTokenRepository());
	}


But now instead of the user info json, I get this, when making a GET request to http://localhost:8999/auth/user

<html>
    <head>
        <title>Login Page</title>
    </head>
    <body onload='document.f.username.focus();'>
        <h3>Login with Username and Password</h3>
        <form name='f' action='/auth/login' method='POST'>
            <table>
                <tr>
                    <td>User:</td>
                    <td>
                        <input type='text' name='username' value=''>
                    </td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td>
                        <input type='password' name='password'/>
                    </td>
                </tr>
                <tr>
                    <td colspan='2'>
                        <input name="submit" type="submit" value="Login"/>
                    </td>
                </tr>
                <input name="_csrf" type="hidden" value="30e1316c-34ed-4731-b8b2-d405f11f7b78" />
            </table>
        </form>
    </body>
</html>
joshuawhite929 (13) [Avatar] Offline
#3
Help me understand where you are in this chapter. From the beginning, I can run the following without a problem. Note, I am using a Mac and I am using HTTPie (see: https://httpie.org) from the command line. I am able to execute the following using the "master" branch:


mvn clean package docker:build


docker-compose -f docker/common/docker-compose.yml up





For user: john.carnell
Get an "access_token" for john.carnell:
http -a eagleeye:thisissecret -f POST http://localhost:8901/auth/oauth/token grant_type=password scope=webclient username=john.carnell password=password1


Using the "access_token" returned from executing the above (note, yours will be different):
http :8901/auth/user 'Authorization:Bearer e710efc3-4ab5-4798-85d4-98eb4a5f2a81'



For user: william.woodward
Get an "access_token" for william.woodward:
http -a eagleeye:thisissecret -f POST http://localhost:8901/auth/oauth/token grant_type=password scope=webclient username=william.woodward password=password2


Using the "access_token" returned from executing the above (note, yours will be different):
http :8901/auth/user 'Authorization:Bearer 33b5b4ad-6271-49ca-a3d7-3e027f51f54d'




These seems to work fine. I noticed the port you are using is different. Can you double check that?
461784 (3) [Avatar] Offline
#4
Hi,

this is my stackoverflow question: http://stackoverflow.com/questions/43519278/spring-cloud-oauth2authentication-returns-nullpointerexception

Minus the above two methods that I have commented out.