|
72 | 72 | 3. Visit http://localhost:8080/ to see the result.
|
73 | 73 |
|
74 | 74 | *You can configure the client's username in the application.yml.*
|
75 |
| -
|
76 |
| -## With Bearer auth security |
77 |
| -
|
78 |
| -1. Try the security-grpc-bearerAuth-server example first run: |
79 |
| -
|
80 |
| - ````sh |
81 |
| - ./gradlew :example:security-grpc-bearerAuth-server:bootRun |
82 |
| - ```` |
83 |
| -
|
84 |
| -2. In a different terminal window run: |
85 |
| -
|
86 |
| - ````sh |
87 |
| - ./gradlew :example:security-grpc-bearerAuth-client:bootRun |
88 |
| - ```` |
89 |
| -
|
90 |
| -3. Visit http://localhost:8080/ to see the result. |
91 |
| -
|
92 |
| -This will not run out of the box since one needs to set up an identity provider service, like |
93 |
| -for example Keycloak. Keycloak provides an endpoint to retrieve the necessary configuration (Public RSA key, etc). |
94 |
| -The URI to this endpoint needs to be provided in the server's `SecurityConfiguration.java` in the `jwtDecoder()` method. |
95 |
| -
|
96 |
| -Additionally you will need to obtain a valid access token from the Keycloak server. This token has to be provided in |
97 |
| -the client's `SecurityConfiguration.java` |
98 |
| -
|
99 |
| -To obtain an access token you can use Postman and perform an HTTP POST call to: |
100 |
| -`http://127.0.0.1:8080/auth/realms/YOURREALM/protocol/openid-connect/token` |
101 |
| -with basic authentication. Username and password are the client id and secret of the client you configured in the |
102 |
| -Keycloak admin panel (http://127.0.0.1:8080/). |
103 |
| -
|
104 |
| -*You can configure the bearer token in the `SecurityConfiguration.java`* |
105 |
| -
|
106 |
| -**Advice for testing/development:** |
107 |
| -
|
108 |
| -When testing/developing it is not always possible to have an IDP service ready. In that case you can add the following |
109 |
| -line: |
110 |
| -
|
111 |
| -````java |
112 |
| -providers.add(anonymousAuthenticationProvider()); |
113 |
| -```` |
114 |
| -
|
115 |
| -right above (your actual authentication providers) |
116 |
| -
|
117 |
| -````java |
118 |
| -providers.add(jwtAuthenticationProvider()); |
119 |
| -```` |
120 |
| -
|
121 |
| -in the `authenticationManager()` bean method of the server's `SecurityConfiguration.java` |
122 |
| -This will of course require an appropriate Bean like such: |
123 |
| -
|
124 |
| -````java |
125 |
| -@Bean |
126 |
| -AnonymousAuthenticationProvider anonymousAuthenticationProvider() { |
127 |
| - return new AnonymousAuthenticationProvider("dev"); |
128 |
| -} |
129 |
| -```` |
130 |
| -
|
131 |
| -and in the authenticationReader() Bean replace the return with: |
132 |
| -
|
133 |
| -````java |
134 |
| -return new AnonymousAuthenticationReader("dev", "developer", AuthorityUtils.createAuthorityList("ROLE_TEST")); |
135 |
| -```` |
136 |
| -
|
137 |
| -You can add/change the roles there to your liking. |
138 |
| -
|
139 |
| -Overall what happens here is that the BearerAuthenticationReader is replaced by AnonymousAuthenticationReader, which |
140 |
| -ignores the Bearer token and creates an AnonymousAuthenticationToken which is processed by the |
141 |
| -`AnonymousAuthenticationProvider`. This way you can temporarily bypass the bearer token auth. |
142 |
| -You might want to toggle this behavior with a `dev` or `debug` property. |
0 commit comments