Spring cloud security services combat micro -3-4 API authentication security mechanism of (1)

This section began to talk about things related to certification, precautions, corresponding solutions to problems arise.

To write user registration service, some users registration information into it. Registration is also part of our security system

registered

UserController inside the create method


to modify entity classes, together with username and password

because we have configured the generate-ddl within the configuration file is true, will be automatically activated when jpa these two properties added to the database, so we do not own database to manually Riga property.

Set the primary key strategy here is set according to the type of policy the current database, such as the current database is mysql, mysql database it will follow a policy. From the growing field strategy.

strategy


United States [ 'strætədʒi]
UK [' strætədʒi]

  • n strategies; strategic; planning; strategic plan
  • Mode network policy; strategy; strategy

Objects created userInfo

Copy the user of this class, a change of name is called userInfo.
Here the @Entity removed. Because it is not an entity that is not in the database tables and correspondence. UserInfo request and response used to package our service.
Reservations @Data notes, this time to help us generate get and set methods.


UserController everything inside the place of use have changed user UserInfo




So why change it so? Both objects from now, the property is exactly the same, why should it be divided into two? This is more common programming inside a concept, separation of concerns.
Also known as single responsibility principle. Any of a class or method it should only focus on one thing, or is only responsible for one thing, and only when this thing changed, I need to change the class or the method, if we use the User class as input Controller or output , then the User is responsible for this is actually two things. The first thing is the database table with what I do mapping, object database which is reflected in our field, that field is the primary key, and so this information, it is responsible to do with the database table mapping.
And we Controoler his way inside with ginseng and ginseng are actually the input and output services. This is actually two concepts. If we use the same class on both concerns. User are used, then the User is responsible for things will change much. This class is responsible things increases will lead, things change every time I have to go and change User, some problems will occur.
For example, when the database structure changes, I'm going to change the User, when users create or modify a user's service occurred when its input and output can be changed, I need to go and change the User
. This time User class of its responsibilities is not clear. This is not a good programming concepts.

For example, such a user table inside plus integral field. This is a very common requirement.


If I added the integration field, which is also used in Controller User object as an input or output, it means that the field I want to register your group which can have points field. But the time of registration, basic information is not asking you to fill in the points, right? ? ? So that's the reason why we built this UserInfo object. This is the separation of concerns.

We should also note that at the time of programming. It will let you design a good program.

Service Layer Service


That is, we often say that the Service layer.



In the Create Service implementation class






controller layer is exposed app service, the app service to come to the service layer, service layer is again calling DAO do Repository database operations. Such duties completely distinguish the
service layer is responsible for implementing the business logic, Repository responsible for the operation of the database, Repository will not have any business logic in general

method in the declaration service. And the controller are corresponding to

the class Impl Service, which implement these methods.

The statement said the implementation class of Spring Service object, that is, a Bean Spring of


calls within the Service Layer Controller method


to write the Create method. BeanUtils using methods which, passed in the value of UserInfo objects are copied to the User object inside.

The user id after the object is stored, copied to the info object, and then return back. The day before yesterday so that you can get the ultimate creation of the user id is.

Such a simple registration on the finished

Run the test


Status code 200 is returned. The information returned is the time to fill out the registration information, as well as id database generated primary key final increment of the

log which prints the insert statement

recorded in the database

Let's continue to write authentication logic

First clear a few concepts, we are now talking about the certification, not landing.

Certification is to verify the identity of a user of the legality of such a process. Certification to this thing no matter did not succeed, it must go down. It is not the same landing. Once landed there are problems cut off. For example, when you log in user name and an erroneous. So I'll just throw an exception, and will not get any cheaper. Certification time, if you passed in as a user with questions still have to go down, go to the following audit record, you record the results of the identity of what it was like. Eventually the request is not to be adopted, to be determined by the authorization. Not determined by the certification.
Well, clearly these concepts. You can write code.














 

Guess you like

Origin www.cnblogs.com/wangjunwei/p/11902106.html